expressjs / body-parser

Node.js body parsing middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fails silently if the json has extra whitespace

ORESoftware opened this issue · comments

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

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.

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