Raw HTTP Request

This should allow you to access the raw HTTP body of a request for things like validating signatures where the regular HTTP-IN node doesn't work as the request is already parsed by express. Its very basic and crude and probably should't be used for high volume requests, in effect its implementing an HTTP server in a Node-RED flow! This is mostly just a proof of concept to see if I could do it!

The initial request to /foo will be redirected to the same host on port 1882 which is then a TCP IN node listening for any traffic.

That then parses the HTTP request using http-Z and puts the output into msg.payload including a rawBody object which will contain the unparsed body. Headers can be accessed in msg.payload.headers.

This assumes that your Node-RED instance can export port 1882 as well as whatever the HTTP node is already running on, there's no SSL on this connection, but you could implement that using the TCP nodes SSL config or with a reverse proxy in front of Node-RED.

[{"id":"3a7824226fcfa66d","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"833db30a8cb93f9c","type":"http in","z":"3a7824226fcfa66d","name":"","url":"/foo","method":"post","upload":false,"swaggerDoc":"","x":180,"y":180,"wires":[["cb9236ba739c664e"]]},{"id":"f0a3fead9252c48a","type":"http response","z":"3a7824226fcfa66d","name":"","statusCode":"","headers":{},"x":670,"y":180,"wires":[]},{"id":"798019ce7bc287c6","type":"tcp in","z":"3a7824226fcfa66d","name":"","server":"server","host":"","port":"1882","datamode":"stream","datatype":"utf8","newline":"","topic":"","trim":false,"base64":false,"tls":"","x":200,"y":320,"wires":[["c2ab47e157cea238","93d1a184c7381891"]]},{"id":"eeacbf575d4041f8","type":"debug","z":"3a7824226fcfa66d","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":680,"y":320,"wires":[]},{"id":"e8d7a1526a7361cf","type":"tcp out","z":"3a7824226fcfa66d","name":"","host":"","port":"","beserver":"reply","base64":false,"end":false,"tls":"","x":230,"y":460,"wires":[]},{"id":"c2ab47e157cea238","type":"template","z":"3a7824226fcfa66d","name":"Send 200 OK","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"HTTP/1.1 200 \nConnection: Close\nContent-Length: 2\n\nOK","output":"str","x":220,"y":400,"wires":[["e8d7a1526a7361cf"]]},{"id":"93d1a184c7381891","type":"function","z":"3a7824226fcfa66d","name":"Parse Request","func":"let rawBody = msg.payload.split('\\r\\n\\r\\n')[1]\nnode.warn(msg.payload)\nlet r = httpZ.parse(msg.payload);\n\nlet h = {}\nfor (let i = 0; i < r.headers.length; i++) {\n    let n = {}\n    n[r.headers[i]['name']] = r.headers[i]['value']\n    Object.assign(h, n);\n}\nr.headers = h\nr.rawBody = rawBody\nmsg.payload = r\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"httpZ","module":"http-z"}],"x":420,"y":320,"wires":[["eeacbf575d4041f8"]]},{"id":"cb9236ba739c664e","type":"function","z":"3a7824226fcfa66d","name":"Redirect to Port 1882","func":"let hostname = msg.req.headers.host.split(\":\")[0]\nmsg.headers ={}\nmsg.headers.location = `http://${hostname}:1882${msg.req.url}`\nmsg.statusCode = 307\nmsg.payload = ''\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":180,"wires":[["f0a3fead9252c48a"]]}]

Flow Info

Created 1 year, 3 months ago
Rating: 5 1

Owner

Actions

Rate:

Node Types

Core
  • debug (x1)
  • function (x2)
  • http in (x1)
  • http response (x1)
  • tcp in (x1)
  • tcp out (x1)
  • template (x1)
Other
  • tab (x1)

Tags

Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option