不知道发生了什么。当我执行以下代码时...它运行得很好。但它正在产生一个错误。如果我将以下内容粘贴到我的浏览器地址栏中并点击它,我会得到一个URL。如果我将相同的URL放入KRL http: get,我会得到一个完全不同的url。
"http://tinyurl.com/api-create.php?url=http://insideaf.blogspot.com“
我自己在浏览器中得到:http://tinyurl.com/6j7qucx
当通过http:get运行时,我得到:http://tinyurl.com/4fdtnoo
不同之处在于,第二个是通过KRL http:get运行的,它命中请求的站点,但它在请求的末尾附加了一个"/&“。无论我在哪个网站上,它都能做到这一点。如果我在www.google.com上,它会返回一个指向www.google.com/&的tinyurl,这会给我一个错误。我传递给http:get方法的所有站点在最后都会返回一个&。下面是我的代码,这样您就可以看到我并不是不小心自己添加的。
myLocation =事件:参数(“location”);
url2tiny = "http://tinyurl.com/api-create.php?url="+myLocation;
tinyresponse = http:get(url2tiny);
tinyurl = tinyurl.pick("$.content");
如果我对url2tiny执行console.log操作,它看起来完全应该是这样的。似乎当我将url2tiny传递给http:get时,在从tinyurl api请求它之前,它会自动将&添加到它的末尾。
有什么解决这个问题的办法吗?这似乎是http:get方法中的一个错误。如果我错了(我希望我错了),请给我指出正确的方向。
发布于 2011-02-24 01:57:41
在这两种情况下,您的格式都略有偏差。get可以用作pre块中的表达式,但是语法与您在action块中使用它的方式不同。
实际上,您可以通过许多不同的方式发出此请求。传统的方法是通过数据源
数据源
global {
datasource tiny_url_request <- "http://tinyurl.com/api-create.php";
}
rule using_datasource is active {
select when pageview ".*" setting ()
pre {
myLocation = page:env("caller");
thisTiny = datasource:tiny_url_request("?url="+myLocation);
}
{
notify("URL", myLocation) with sticky = true;
notify("datasource: ", thisTiny) with sticky = true;
}
}另一种方法是通过http:get作为pre块中的表达式进行尝试。作为函数调用,http:get有两个必需的参数和两个可选的参数:
http:get(url,params,headers,response_headers );
您的第一次尝试不包括参数。
tinyresponse = http:get(url2tiny)
第二次尝试将参数放在错误的参数位置。
http:get("tinyurl.com/api-create.php";,{"url":myurl})
http:get (预块)
rule get_in_pre is active {
select when pageview ".*" setting ()
pre {
myLocation = page:env("caller");
tinyurl = http:get("http://tinyurl.com/api-create.php", {"url":myLocation});
turl = tinyurl.pick("$.content");
}
{
notify("http:get as expression",turl) with sticky = true;
}
}第三种方法是使用http:get作为操作并自动引发事件
get http:get()
rule using_action is active {
select when pageview ".*" setting ()
pre {
myLocation = page:env("caller");
}
http:get("http://tinyurl.com/api-create.php") setting (resp)
with
params = {"url" : myLocation} and
autoraise = "turl_event";
}
rule get_event is active {
select when http get label "turl_event" status_code "(\d+)" setting (code)
pre {
a = event:param("content");
}
notify("Autoraised from action",a) with sticky = true;
}以下是针对此页面执行的这些规则的示例

https://stackoverflow.com/questions/5069651
复制相似问题