Simulating Http Request Node

Since the planner has been deprecated how exactly would i go about simulating the behavior the the http request node?
I know that i want my http page to hand the data us POST so i know i need to use:
http.post(url, headers, string).sendsync();
I know i have a a valid url on my server that should handle post data.
I am not sure what headers to use, i know that with the node:
{"Content-Type": "application/x-www-form-urlencoded"}
and the string is going to be just a regular string with no special characters.
The node generated target, channel, and value POST data, so i would be able to access the data via
$_POST['target'], $_POST['channel'], and $_POST['data']

but when i try something like:
agent:
device.on("sendData", function() { http.post("http://www.exampleurl.com", {"Content-Type": "application/x-www-form-urlencoded"}, "sample data").sendsync(); } );
device:
agent.send("sendData");

Yet there is no POST data sent to the page.

I believe you need to send something in the agent.send otherwise it’d error, ie

agent.send(“sendData”, “data”);

and

device.on(“sendData”, function(d) { … }

…but beyond that someone who knows more about http requests should answer :slight_smile:

i knew that, i was just trying to send in a sample string to see if i could even recieve it in my php. or is the data specially encoded when sent through the function? I figured it would just be a string.

Both this line:
device.on("sendData", function() {
and this line:
agent.send("sendData");
produce Squirrel run-time errors due to the lack of a parameter for the sent data:

`2013-10-24 20:39:35 UTC+1: [Agent] bad callback for device.on: must take 1 parameter
2013-10-24 20:39:35 UTC+1: [Agent] at main:1

2013-10-24 20:39:36 UTC+1: [Device] ERROR: bad parameters to agent.send(string,data)
2013-10-24 20:39:36 UTC+1: [Device] ERROR: at main:27`

Squirrel execution stops when a run-time error occurs; perhaps this is why you never saw the expected POST?

Peter

Even with this code i dont recieve any POST data on the desired page; the url in this code is not real but i use an active one in my actual code:
AGENT:

// Basic wrapper to create an execute an HTTP POST device.on("HttpPostWrapper", function(string) { local request = http.post("http://www.example.com/doData.php", {"Content-Type": "application/x-www-form-urlencoded"}, string); local response = request.sendsync(); return response; } );

DEVICE:

local dataToSend = format("t=%.4fsg=%.5fv=%.2fwe=%iid=%s",temp, sg, voltage, weight, "deviceID"); agent.send("HttpPostWrapper",dataToSend );

Well, that format doesn’t produce valid application/x-www-form-urlencoded data (there’s no ampersands, for a start). You can either use http.urlencode() to produce valid strings, or debug your own implementation using PHP’s $HTTP_RAW_POST_DATA.

Peter

so your saying this should work?:

http.urlencode(format("t=%.4f&sg=%.5f&v=%.2f&we=%i&id=%s",temp, sg, voltage, weight, "deviceID"));

I figured it all out this is the solution code that fixed my problem:

agent:
device.on("HttpPostWrapper", function(data) { local dataToSend = http.urlencode({t=format("%.4f", data.temp),sg=format("%.5f", data.sg),v=format("%.4f", data.volt),we=format("%i", data.weight),id=format("%s", data.id)}); local request = http.post("http://www.thebeerbug.com/extras/IMPTest/impSendData.php", {"Content-Type": "application/x-www-form-urlencoded"}, dataToSend); local response = request.sendsync(); return response; } );
device:
agent.send("HttpPostWrapper", {temp=read_int_temp(), sg=calcGrav(), volt=read_battery(), weight=read_weight(), id="20000c2a69022918"});
In the end the problem was my ignorance to the POST http encoding. Thanks for all the help guys

Really appreciate you posting the solution. I was able to make it work too by using your code as guidance.

FYI, since I don’t have access to “theBEERbug” (although I checked out the site), I replace the line URL with “Requestb.in” so my line looks like this:

local request = http.post(“http://requestb.in/1jc2jfp1?ImpTest=Success”, {“Content-Type”: “application/x-www-form-urlencoded”}, dataToSend);

Just go to http://requestb.in to get your own ID

local request = http.post(“http://requestb.in/<>?<>”, {“Content-Type”: “application/x-www-form-urlencoded”}, dataToSend);

Thanks for helping me learn.