Http.post problem

I’m trying to send data to a web browser. What I don’t understand is how to incorporate the header? I managed to get the header of the site I want to send data to. What do I need from here? This is the header info:

POST /imp.html HTTP/1.1[CRLF] Host: glorincz.netai.net[CRLF] Connection: close[CRLF] User-Agent: Web-sniffer/1.1.0 (+http://web-sniffer.net/)[CRLF] Accept-Encoding: gzip[CRLF] Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF] Cache-Control: no-cache[CRLF] Accept-Language: de,en;q=0.7,en-us;q=0.3[CRLF] Referer: http://web-sniffer.net/[CRLF] Content-type: application/x-www-form-urlencoded[CRLF] Content-length: 0[CRLF]

Why does this piece of code in the agent doesn’t work?

function incoming(pinState){ server.log("incoming message from the device! "+pinState); local postUrl = "http://yyy.xxx.com/imp.html"; local header ={"Content-Type": "application/x-www-form-urlencoded"}; http.post(postUrl,header, "some_stuff_to_post"); local res = req.sendsync(); } device.on("ledState", incoming);

I am posting to a server in my mailgun code, maybe you can adabt that to what you need?

Actually, just couldn’t stop… Try this

`function postStuff()
{
local request = http.post(“https://server.com”, {“Content-Type”: “application/x-www-form-urlencoded”}, “field1=value&field2=value”);

local response = request.sendsync();
server.log("Response: " + response.body);
}`

I’m not totally sure I understand what you’re trying to accomplish. What do you mean by “I’m trying to send data to web browser”?

Are you trying to keep a webpage synced with some information from the imp? Are you trying to send the information to an external webservice, etc?

Well, I have a radio button and I want to make sure that the buttons’ state is synced to the output pin. In other words, when I press the button in the browser, I want to see the state of the digital out pin (1 or 0). So far I managed to send the state with ‘.read()’ to the agent, now I want to pass this on and display it in the site.

You could show json from the agent, which the site then pulls using javascript. I think I saw an example somewhere on how to do that?

I checked the http.jsonencode(object) and to all fairness it doesn’t really help. I clearly remember that about two years ago I could easily display data coming from the imp. There was even example code for that. Why is it so damn hard and why isn’t there better documentation???

So how do I change the header in my html page?

A html file is static, you can’t change anything in it, and it does not have headers.

You either have to have some kind of system like php to receive the post form the agent and process it, or do it the other way around with javascript where it reads json (can be something else too, but I find json easy), and then sets the buttons according to what is received.

Thank you!

@alkopop79 - You can do this fairly easily with PubNub.

Here’s an example.

Here’s a blog post that goes along with it.

the way I have done this is to use a technique called long-polling. I don’t know how much street cred this has but it has worked. (I am a beginner)

The web page submits a GET request upon loading and the agent takes this but does not respond right away, it stores the response.

When the device has an action it notifies the agent which then sends a response to the GET request.

Every time the web page gets a response to a GET, it immediately opens another GET request. This way there is always a request open and responses are immediate.

With some browsers this can create a ‘constantly loading’ situation but when I use Chrome everything appears to run smoothly.

I use AJAX and JQUERY.

I don’t know much about Javascript to be able to do it myself and weirdly there isn’t any example. I tried the pubnub way and it doesn’t work. I guess I should just give my original idea up.

Kicking something together this weekend…

I would very much appreciate that!

Just noticed… I already wrote a bit about this…

Fantastic, thanks!!! It worked straight away!