Anyone figured out how to read the response from a HTTP request yet?
The wiki says:
"If the response body is valid JSON (e.g. “foo”, 23, or [1,2,3]), it will
be parsed and emitted from the “response” output port on the block.
This allows you to asynchronously make requests to external services and
act on the response."
Maybe I’m being an idiot (it happens quite often) but I can’t see how this is supposed to be done. Sorry!
Anybody?
Have it working you need to define an output port
@controlCloud - thanks buddy. I’m doing that just fine - but how to I parse the servers response of that request, back on the imp side of things inside an InputPort handler?
class RespInput extends InputPort
{
name = "response";
type = "string";
function set(serverresponse)
{
server.show("Our server replied "+serverresponse);
server.log("SERVER REPLY "+serverresponse);
}
}
local input = RespInput("response", "string");
then use the input like
imp.configure("App name", [input], [output]);
drazvan, thanks just assumed you had to use a number as that’s what in Vimp source output type in the planner.
thanks @drazvan @controlCloud.
My code is pretty much exactly the same and so im guessing im doing something else wrong
my InputPort set method never seems to get called even though im successfully making a http request and am getting a 200 response according to the http request node in the planner.
I’ve tried adding a noodle back to my imp, from the http request node - but still no joy.
Maybe the response from my server needs to be in a particular format or have certain http headers?
I’ve tried a JSON response too rather than just an empty http response as it mentioned this on the wiki - but again no luck.
Here’s a screen grab of my planner view - anybody spot anything obvious? You’ll notice I’ve added a SHOWINPUT node that seems to be displaying my request vars that I’m sending as a parameter in my OutputPort set method.
Here’s my code below, any pointers much appreciated!
class RespInput extends InputPort
{
name = “response”;
type = “string”;
function set(serverresponse)
{
server.show("Our server replied "+serverresponse);
server.log("SERVER REPLY "+serverresponse);
}
}
local input = RespInput(“response”, “string”);
local output = OutputPort(“request”, “string”);
//then use the input like
imp.configure(“Jon Test”, [input], [output]);
server.show(“making request…”);
output.set(“requestvars”);
What is your server sending as a response? If you want to send a simple string, just put it between quotes, like “response”. It works fine for me, the server returns a quoted string and I am getting it back in the set() method of the input port. Content type in my case is text/plain (not sure if that makes a difference).
@drazvan - I’ll try playing with different content types using my own server when I get home and see if that makes a difference, but for the moment the url I’m using is a requestb.in url that has a content-type of text/html; charset=utf-8 returns and is actually returning “ok”. url below:
Well, your URL above returns ok (without any quotes around it). You actually need it to return “ok” (with the quotes).
@controlCloud @drazvan thanks guys for all your help, awesome imp community already
Your insight helped me figure out that I was pretty much doing the same thing as you guys and pointed me in the direction of the thing that was probably different - my server.
I’ve been using a Google App Engine servlet I wrote to handle the requests and it turns out that App Engine seems to not like my imp requests, because the same servlet works on my dedicated machine. Something in the headers I think.
Hmmm… I’ll figure out exactly what was going on tomorrow and post back just in case anybody else tries this!