Skip to content
This repository has been archived by the owner on Oct 11, 2022. It is now read-only.

req.body = {} even though req._readableState.buffer = the yummy data #142

Open
MichaelJCole opened this issue Sep 7, 2017 · 0 comments
Open

Comments

@MichaelJCole
Copy link

MichaelJCole commented Sep 7, 2017

Hi, I'm unable to get req.body in my routes correctly.

JsonRoutes.Middleware.use('/api/*', oAuth2Server.oauthserver.authorise());  
JsonRoutes.ErrorMiddleware.use('/api/*', RestMiddleware.handleErrorAsJson);

// couple other routes

JsonRoutes.add('post', '/api/hooks', function(req, res, next) {
  // Access Control
  var accessToken = findAccessToken(req);
  console.log(req.url, req.body, req);   // <--- here, req._readableState.buffer below

  // Upsert
  var target_url = req.body.targetUrl;
  ZapierSubscriptions.upsert({target_url}, { $set: {
    target_url,
    brokerId: accessToken.userId,
    teamId: req.body.teamId,
    event: req.body.event,
  } }); // throws
  JsonRoutes.sendResult(res, { code: 201 });
});

In the console I get a very long req, but towards the top this:

2017-09-07T00:25:20.043133+00:00 app[web.1]: /api/hooks {} IncomingMessage {
2017-09-07T00:25:20.043152+00:00 app[web.1]:   _readableState: 
2017-09-07T00:25:20.043153+00:00 app[web.1]:    ReadableState {
2017-09-07T00:25:20.043154+00:00 app[web.1]:      objectMode: false,
2017-09-07T00:25:20.043155+00:00 app[web.1]:      highWaterMark: 16384,
2017-09-07T00:25:20.043157+00:00 app[web.1]:      buffer: [ <Buffer 7b 22 74 61 72 67 65 74 55 72 6c 22 3a 22 68 74 74 70 73 3a 2f 2f 7a 61 70 69 65 72 2e 63 6f 6d 2f 68 6f 6f 6b 73 2f 73 74 61 6e 64 61 72 64 2f 32 34 ... > ],
2017-09-07T00:25:20.043157+00:00 app[web.1]:      length: 139,

The buffer is hex for the POST body I'm sending. I don't see a body parser needed in the examples. this line seems to take care of parsing the JSON.

How to get req.body?

Here's my workaround:

JsonRoutes.add('post', '/api/hooks', function(req, res, next) {
  // Access Control
  var accessToken = findAccessToken(req);

  // Fix for missing body
  if (_.isEmpty(req.body)) req.body = JSON.parse(req._readableState.buffer.toString('utf8'));

  //...

Thanks!

@MichaelJCole MichaelJCole changed the title req.body = {} even though req._readableState.buffer = the yummy data req.body = {} even though req._readableState.buffer = the yummy data Sep 7, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant