-
• #2
It's passed at the end of the request like this:
var options = { ... } var data = JSON.stringify({"key":"value", ...}); require("http").request(options, function(res) { ... }).end(data);
-
• #3
Make sure to add the appropriate headers when sending JSON data:
var data = JSON.stringify({ key: "value" }); const options = { //... headers: { "Content-Type": "application/json", "Content-Length": json.length } }; require("http").request(options, function(res) { //... }).end(data);
I recommend attaching an error handler :)
const req = require("http").request(/* ... */); req.on("error", err => console.log("Request error: " + err.message)) ; req.end(data);
-
• #4
Thanks - I'll make a note to add this to: http://www.espruino.com/Internet
Just in case there's any interest in going the other way (handling POSTed data as a server) that one does have some example code here: http://www.espruino.com/Posting+Forms
From espruino htttp reference:
But where do I attach some JSON data (body/payload) to the POST request ?