question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

http-in not handling post error if headers set to application/json and non-json received

See original GitHub issue

If an http-in is set to accept POST input - and the sender sets the header to application/json - but then sends a payload that is NOT json… the error is not surfaced via the catch node…

Error: invalid json at parse (/Users/*****/node-red/node_modules/express/node_modules/connect/node_modules/body-parser/lib/types/json.js:64:15) at /Users/****/node-red/node_modules/express/node_modules/connect/node_modules/body-parser/lib/read.js:91:18 at IncomingMessage.onEnd (/Users/****/node-red/node_modules/express/node_modules/connect/node_modules/body-parser/node_modules/raw-body/index.js:136:7) at IncomingMessage.g (events.js:180:16) at IncomingMessage.emit (events.js:92:17) at _stream_readable.js:944:16 at process._tickCallback (node.js:442:13)

simple flow to demo this is

[{"id":"3ca4b708.c35b48","type":"http in","name":"","url":"/dummy","method":"post","swaggerDoc":"","x":170.88888549804688,"y":668.5555579044202,"z":"f307b843.0cf848","wires":[["a4b95451.5b46a8"]]},{"id":"12a3e242.ed5c1e","type":"catch","name":"","x":195.88888549804688,"y":712.5555579044202,"z":"f307b843.0cf848","wires":[["629ea4db.9d615c"]]},{"id":"a4b95451.5b46a8","type":"debug","name":"","active":true,"console":"true","complete":"true","x":402.8888854980469,"y":669.5555725097656,"z":"f307b843.0cf848","wires":[]},{"id":"629ea4db.9d615c","type":"debug","name":"","active":true,"console":"false","complete":"true","x":420.8888854980469,"y":713.5555725097656,"z":"f307b843.0cf848","wires":[]},{"id":"c135fd63.3eca","type":"inject","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":93.88888549804688,"y":618.5555725097656,"z":"f307b843.0cf848","wires":[["1e8f148d.e170eb"]]},{"id":"9de7fa99.621808","type":"debug","name":"","active":true,"console":"false","complete":"true","x":575.888916015625,"y":616.5555267333984,"z":"f307b843.0cf848","wires":[]},{"id":"386967b0.c79698","type":"http request","name":"","method":"POST","ret":"txt","url":"localhost:1880/red/dummy","x":408.8888854980469,"y":616.5555267333984,"z":"f307b843.0cf848","wires":[["9de7fa99.621808"]]},{"id":"1e8f148d.e170eb","type":"function","name":"add header","func":"msg.headers= {\"Content-Type\":\"application/json\"};\nreturn msg;","outputs":1,"noerr":0,"x":245.88888549804688,"y":617.5555267333984,"z":"f307b843.0cf848","wires":[["386967b0.c79698"]]}]

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
knollearycommented, Oct 3, 2015

Just to confirm, this is still an issue after the move to express 4

0reactions
livioalvescommented, Aug 9, 2019

Just change function init from file red/api/index.js

from adminApp.use(bodyParser.json({limit:maxApiRequestSize})); to ` this.addRawBody = function (req, res, buf, encoding) { req.rawBody = buf.toString(); }

    adminApp.use((req, res, next) => {
            bodyParser.json({ 
                limit:maxApiRequestSize,
                verify: this.addRawBody
            })(req, res, (err) => {
                if (err) {
                    console.log(err);
                    res.sendStatus(400);
                    return;
                }
                next();
            });
        }
    );

`

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP POST with Json on Body - Flutter/Dart - Stack Overflow
i would love to know how i can parse the json right and insert into body of the request. this is the header...
Read more >
Compose HTTP requests and handle errors - Microsoft Learn
Every request should include the Accept header value of application/json , even when no response body is expected. Any error returned in the ......
Read more >
HTTP Requests and Responses
This section explains how to formulate HTTP requests and read HTTP responses. HTTP Request Headers. Set an Accept: application/json header on all GET...
Read more >
HTTP Client - Laravel - The PHP Framework For Web Artisans
To make requests, you may use the head , get , post , put , patch , and delete ... By default, data...
Read more >
Node-Red HTTP Request Node for Beginners
The screen shot below shows how to set the content type to JSON. ... Note: If the port is non standard (not 80)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found