HTTP POST Content Length 0 on HTTP Request Node

2,451 views
Skip to first unread message

Devin Wilson

unread,
Feb 4, 2014, 2:39:38 AM2/4/14
to node...@googlegroups.com
I am trying to send a post request to a webserver on an Arduino at 130.102.86.134 and through using Charles to view the HTTP traffic, I find that node red is sending post requests with content length 0. Below is my JSON string and raw request data

[{"id":"98b88f67.67477","type":"inject","name":"","topic":"","payload":"{\"pin\": 8, \"value\": 1 }","repeat":"","crontab":"","once":false,"x":116,"y":268,"z":"6bd7d190.94283","wires":[["d1596f41.2ea69","845266a8.7bad98"]]},{"id":"d1596f41.2ea69","type":"http request","name":"LED on","method":"POST","url":"http://130.102.86.134/pins/update","x":403,"y":281,"z":"6bd7d190.94283","wires":[["65e83590.9a17cc"]]},{"id":"845266a8.7bad98","type":"debug","name":"","active":true,"complete":"true","x":390,"y":106,"z":"6bd7d190.94283","wires":[]},{"id":"65e83590.9a17cc","type":"debug","name":"","active":true,"complete":"true","x":630,"y":274,"z":"6bd7d190.94283","wires":[]}]

POST /inject/16503aae.e9afc5 HTTP/1.1
Host: localhost:1880
Content-Length: 0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: JSESSIONID=5e56ff866f0b0e308f802b997a45; treeForm_tree-hi=treeForm:tree:applications:iLabServiceBroker

Am I doing something wrong using Node Red or is this an issue with the http request node? The path of the post doesn't appear to match the post I specified in node red.

Nicholas O'Leary

unread,
Feb 4, 2014, 4:36:12 AM2/4/14
to node...@googlegroups.com
Hi Devin,

you have captured the HTTP post from the browser back to Node-RED used to trigger the Inject node - a 0-length POST to /inject/[id of your inject node].

This is not the HTTP post request to your arduino.

Regards,
Nick


--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Henrik Olsson

unread,
Feb 7, 2014, 5:57:03 AM2/7/14
to node...@googlegroups.com
Hi Devin!
I think you shall try Wireshark (http://www.wireshark.org/) to sniff your wire trafic.

Henrik

Devin Wilson

unread,
Feb 10, 2014, 11:14:55 PM2/10/14
to node...@googlegroups.com
Thanks Nick,

I've managed to get this post request working by manually setting the msg.headers value in a function block. It seems the arduino was having trouble reading chunked requests (i hadn't made it to handle chunks). By going through the github code i found node-red was sending the request as a chunked post and manually setting the msg.headers resolved this

Charles Palmer

unread,
Feb 17, 2014, 3:50:35 PM2/17/14
to node...@googlegroups.com
Do I have the same problem as Devin? Could Devin post his fix?

My attempt at sending a POST with a node-RED fails - as though the body of the POST does not arrive at the server. How do I fix this?

I am running a local HTTP server from a node.js command line, and it works as expected if I send it a POST message (to http://localhost:1881, using RESTclient installed in my Windows 7 Firefox browser).

I try to POST a message to the server from node-RED. The server prints the headers the console, but not the text associated with the arrival of the body, and after a while the node-RED http request node sends "Error: socket hang up" <snip> statusCode": "ECONNRESET"

The node info text says "payload is sent as the body of the request", and the incoming object does contain a payload element.

What should I be doing differently?

Here is the server code:

var http = require('http');
var server = http.createServer(function(req, res) { 
   if (req.method == 'POST') {
      console.log("POST headers: " + JSON.stringify(req.headers));      
      req.on('data', function(chunk) {
        var rsp = "POST body: " + chunk.toString();
        console.log(rsp);     
        res.end(rsp);           
      })                
    }
  });
server.listen(process.argv[2], function() {
  // argv[2] is port number from command line
});

Here is the node-RED code:

[{"id":"f8ddeb2e.7fe338","type":"inject","name":"POST \"foo\" to \"/bar\"","topic":"bar","payload":"foo","repeat":"","crontab":"","once":false,"x":128.57142639160156,"y":351.4285259246826,"z":"4cf1daa3.af57a4","wires":[["dcc86858.e411f8"]]},{"id":"dcc86858.e411f8","type":"function","name":"Build URL and body","func":"// Send an HTTP POST\n\nvar messageBody = msg.payload;\nvar url = msg.topic;\n\nmsg = {}; \n\nmsg.url = \"http://localhost:1881/\" + url;\nmsg.method = \"POST\";\nmsg.payload = \"Payload is \" + messageBody;\n//var headers = {\"Transfer-Encoding\": \"chunked\"};\n//msg.headers = headers;\nvar headers = {\"content-type\":\"text/plain; charset=UTF-8\", \"connection\":\"keep-alive\", \"pragma\":\"no-cache\",\"cache-control\":\"no-cache\", \"Content-Length\": msg.payload.length};\nmsg.headers = headers;\nreturn msg;","outputs":1,"x":342.8571319580078,"y":349.9999713897705,"z":"4cf1daa3.af57a4","wires":[["ab4af96b.59387","692bcfaa.f629a"]]},{"id":"8b574262.92b8f","type":"debug","name":"HTTP POST Response","active":true,"complete":"true","x":774.2856597900391,"y":318.57140922546387,"z":"4cf1daa3.af57a4","wires":[]},{"id":"ab4af96b.59387","type":"http request","name":"HTTP POST","method":"GET","url":"","x":540.0000381469727,"y":320.00000286102295,"z":"4cf1daa3.af57a4","wires":[["8b574262.92b8f"]]},{"id":"692bcfaa.f629a","type":"debug","name":"POST request","active":true,"complete":"true","x":544.2857131958008,"y":391.428599357605,"z":"4cf1daa3.af57a4","wires":[]}]
You'll need Skype CreditFree via Skype

Nicholas O'Leary

unread,
Feb 17, 2014, 5:20:06 PM2/17/14
to node...@googlegroups.com
Hi Charles,

your issue was different to Devin's

In your flow, the HTTP Request node was configured to do a GET. Your incoming message overrode that, setting it to POST. You hit a bug where we didn't then send the post data in this particular circumstance.

I have just pushed a fix to git. With that, you don't need to touch the request headers at all.

Nick


--

Charles Palmer

unread,
Feb 18, 2014, 5:12:19 AM2/18/14
to node...@googlegroups.com
Thanks for the speedy response Nick. The GET/POST issue was an oversight on my part.

I wonder if you can help me with the next part of the puzzle. Yesterday I ran my POST server from a node.js command line script, as I could not work out how to make a POST server using node-RED. This should be possible, right? In the example below, the HTTP In node does not fire,and the HTTP Request node outputs an error "Error: connect ECONNREFUSED". What have I missed? (This works fine with the server running from a node.js command line).


[{"id":"f8ddeb2e.7fe338","type":"inject","name":"POST \"foo\" to \"/bar\"","topic":"bar","payload":"foo","repeat":"","crontab":"","once":false,"x":128.57142639160156,"y":351.4285259246826,"z":"4cf1daa3.af57a4","wires":[["dcc86858.e411f8"]]},{"id":"dcc86858.e411f8","type":"function","name":"Build URL and body","func":"// Send an HTTP POST.\n\nvar messageBody = msg.payload;\nvar url = msg.topic;\n\nmsg = {}; \n\nmsg.url = \"http://localhost:1881/\" + url;\nmsg.payload = \"Payload is \" + messageBody;\n\nreturn msg;","outputs":1,"x":342.8571319580078,"y":349.9999713897705,"z":"4cf1daa3.af57a4","wires":[["ab4af96b.59387","692bcfaa.f629a"]]},{"id":"8b574262.92b8f","type":"debug","name":"HTTP POST Response","active":true,"complete":"true","x":774.2856597900391,"y":318.57140922546387,"z":"4cf1daa3.af57a4","wires":[]},{"id":"ab4af96b.59387","type":"http request","name":"HTTP POST","method":"POST","url":"","x":549.9999961853027,"y":325.71427154541016,"z":"4cf1daa3.af57a4","wires":[["8b574262.92b8f"]]},{"id":"692bcfaa.f629a","type":"debug","name":"POST request","active":true,"complete":"true","x":544.2857131958008,"y":391.428599357605,"z":"4cf1daa3.af57a4","wires":[]},{"id":"8a5c14e2.24fe38","type":"http response","name":"/bar Server Out","x":705.7142639160156,"y":481.42856788635254,"z":"4cf1daa3.af57a4","wires":[]},{"id":"5e4d40d2.f83a98","type":"http in","name":"/bar Server In","url":"localhost:1883/bar","method":"post","x":167.14285278320312,"y":498.57143783569336,"z":"4cf1daa3.af57a4","wires":[["c122f975.6d0828","2cdb04b5.83573c"]]},{"id":"c122f975.6d0828","type":"debug","name":"POST request","active":true,"complete":"true","x":388.5714111328125,"y":540.0000247955322,"z":"4cf1daa3.af57a4","wires":[]},{"id":"2cdb04b5.83573c","type":"function","name":"/bar Server Processing","func":"// grab the POST body and return it\nvar POSTBody = msg.req.body;\n\nmsg = {};\nmsg.res.body = \"POST body received: \" + POSTBody;\n// or do I send this?\nmsg.payload = \"POST body received: \" + POSTBody;\n\nreturn msg;","outputs":1,"x":428.5714111328125,"y":477.14285373687744,"z":"4cf1daa3.af57a4","wires":[["8a5c14e2.24fe38","4a3ae357.db5974"]]},{"id":"4a3ae357.db5974","type":"debug","name":"POST Response","active":true,"complete":"true","x":714.2857284545898,"y":547.1428394317627,"z":"4cf1daa3.af57a4","wires":[]}]

Nicholas O'Leary

unread,
Feb 18, 2014, 1:48:23 PM2/18/14
to node...@googlegroups.com

The HTTP In nodes listen on the same port as the node-red ui. In their configuration, you only specify the path element.

In your flow, the path of the http in node includes "localhost:1883" which it shouldn't. It should just be /bar. Also, the URL passed to the http request node is "localhost:1881" - the wrong port number.

Nick

--
Reply all
Reply to author
Forward
0 new messages