質問

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.

役に立ちましたか?

解決

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top