http:添加“&”在得到之前

发布于 2024-10-18 10:49:49 字数 1032 浏览 5 评论 0 原文

不知道发生了什么事。当我执行以下代码时...它运行良好...但它产生了错误。如果我将以下内容粘贴到浏览器地址栏中并点击它,我会得到一个 URL。如果我通过 KRL http:get 输入相同的 URL,我会得到一个完全不同的 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,它会返回一个 tinyurl,结果为 www.google.com/& with 给我一个错误。我传递给 http:get 方法的所有站点都会返回一个 &在最后。这是我的代码,以便您可以看到我自己不是无意中添加的。

myLocation = 事件:param("位置");

url2tiny = "http://tinyurl.com/api-create.php?url="+myLocation;

tinyresponse = http:get(url2tiny);

tinyurl = tinyurl.pick("$.content");

如果我console.log url2tiny,它看起来就像它应该的那样。看来当我将 url2tiny 传递给 http:get 时,它会自动添加 &在从tinyurl api 请求它之前到它的末尾。

对于解决此问题有什么想法吗?这似乎是 http:get 方法中的一个错误。如果我错了(我希望我是错的),请指出我正确的方向。

Not sure what is going on. When I perform the following code... it runs fine... but it is producing an error. If I paste the following into my browser address bar and hit it, I get one URL. If I put the same url through the KRL http:get, I get a completely different URL.

"http://tinyurl.com/api-create.php?url=http://insideaf.blogspot.com"

on my own in the browser I get: http://tinyurl.com/6j7qucx

when run through http:get I get: http://tinyurl.com/4fdtnoo

The difference is that the second one, the one run through the KRL http:get hits the requested site, but it appends a "/&" to the end of the request. It does this no matter what site I am on. If I am on www.google.com, it returns a tinyurl that results in www.google.com/& with gives me an error. All sites that I pass to the http:get method get returned with an & at the end. Here is my code, so that you can see that I am not accidentally adding it myself.

myLocation = event:param("location");

url2tiny = "http://tinyurl.com/api-create.php?url="+myLocation;

tinyresponse = http:get(url2tiny);

tinyurl = tinyurl.pick("$.content");

If I console.log the url2tiny, it looks exactly like it should. It appears that when I pass url2tiny to http:get, it is automatically adding the & to the end of it before it requests it from the tinyurl api.

Any ideas for workarounds to this issue? It appears to be a bug in the http:get method. If I am wrong (and I hope I am), please point me in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2024-10-25 10:49:49

在这两种情况下,您的格式都略有偏差。 http:get 可以用作 pre 块中的表达式,但语法与在 action 块中使用它的方式不同。

实际上,您可以通过多种不同的方式提出此请求。传统的方法是通过数据源

DATASOURCE

  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 有 2 个必需参数和两个可选参数:

http:get(url, params, headers, response_headers );

您的第一次尝试不包含参数。
tinyresponse = http:get(url2tiny)

第二次尝试将参数放置在错误的参数位置。
http:get("tinyurl.com/api-create.php";,{"url":myurl})

http:get (pre block)

  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 作为操作并自动引发事件

http:get (action)

  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;
  }

以下是针对此页面执行这些规则的示例
在此处输入图像描述

In both cases, your format is just slightly off. http:get can be used as an expression in the pre block, but the syntax is different from the way that you use it in the action block.

There are actually a number of different ways that you could make this request. The traditional way is through a datasource

DATASOURCE

  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;
    }
  }

The other way is how you were trying and it is through http:get as an expression in the pre block. Called as a function, http:get has 2 required parameters and two optional parameters:

http:get(url, params, headers, response_headers );

Your first attempt did not include the params.
tinyresponse = http:get(url2tiny)

The second attempt places the params in the wrong argument position.
http:get("tinyurl.com/api-create.php";,{"url":myurl})

http:get (pre block)

  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;
    }

  }

The third method is using http:get as an action and auto-raising an event

http:get (action)

  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;
  }

Here is an example of these rules executing against this very page
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文