expressjs / body-parser

Node.js body parsing middleware
MIT License
5.45k stars 727 forks source link

fails silently if the json has extra whitespace #518

Closed ORESoftware closed 8 months ago

ORESoftware commented 8 months ago

this code:

import * as bp from 'body-parser';

app.use((req, res, next) => {
  req.on('data', d => {
    console.log({d: String(d)});
  });
  next();
});

app.use(bp.json({}));

the code runs, but req.body is {} empty, but it shouldn't be

when I uncomment this:

app.use((req, res, next) => {
  req.on('data', d => {
    console.log({d: String(d)});
  });
});

there is plenty of json coming in and it's parseable by jq

here is original command:

d=$(cat './scripts/curl/foo.json')
echo "$d"
curl -X POST -H 'application/json' '0.0.0.0:3900/trace' --data "$d"

the json is fine

wesleytodd commented 8 months ago

I dont think this report has enough information to support the "extra whitespace" mentioned in the title? Am I missing something on this here? I don't see you posting the content of the json file. That said, I also believe the code you posted would have some bugs from reading on the request stream along with using the body parser. I have not tried that in a while, but it just looks to me like this is doing something problematic. If you can provide some more clear explanation that would be super helpful.

jonchurch commented 8 months ago

this will consume the stream of data before bodyparser can hook into it and consume the request's data