-
• #2
That's odd... Could you have a go at adding a 'Content-Length' header?
There are a few examples of POSTing data on the IoT services page: http://www.espruino.com/IoT+Services
and you could have a go at modifying one of those?
-
• #3
OK - so I got the dweet one working OK, but still no luck with mine (including the content-length). Is there any way to see the actual request being made? The request function is never invoking the response callback, so would be nice to se what was on the wire...
cheers
-
• #4
Change your host from
host: 'http://192.168.1.66',
tohost: '192.168.1.66',
and give that a try. -
• #5
Wow, nicely spotted @dave_irvine! :)
You can add an error handler to see what's up with the connection as well if you want to: http://www.espruino.com/Internet#handling-errors
-
• #6
Nicely spotted indeed! All working now - thanks...
For my future reference here's the final code that works (note that the res.on() events are only triggered if the server actually returns something:var WIFI_NAME = ""; var WIFI_OPTIONS = { password : "" }; var wifi = require("EspruinoWiFi"); wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) { if (err) { console.log("Connection error: "+err); return; } console.log("Connected!"); sendData(); }); function sendData() { var content = "{module:'espruino'}"; var options = { host: '192.168.1.66', // host ip (host name works as well) port: 8081, // (optional) port, defaults to 80 path: '/stats/login/', // path sent to server method: 'POST', headers: { "Content-type" : "application/json", "Content-length": content.length} }; var req = require("http").request(options, function(res) { console.log('res',res); res.on('data', function(data) { console.log("HTTP> "+data); }); res.on('close', function(data) { console.log("Connection closed"); }); }); req.on('error',function(err){ console.log(err); }); req.end(content); console.log("Request sent"); }
-
• #7
Was content-length required in the end? Or was it just the URL that @dave_irvine spotted?
-
• #8
It was the URL - adding the content-length didn't help...
Hi Guys - having issues getting an HTTP POST to work. Device is an espruino wifi, flashed to the latest version.
Here's the code:
The connection works OK and I also get the 'request sent' message, but nothing after that on the espruino, and no connection received by the server.
I've verified that the endpoint (which is a server of mine) is working OK (accepts a POST from a REST client), and HTTP.GET works fine (copied one of the examples). I'm doubtless doing something basic wrong, but not sure how to debug further...