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.

Custom http routes don't currently include req.body

See original GitHub issue

Description

Currently when you setup a custom http route (as described in #514), the route callback doesn’t get a req.body field. This is because Express requires you to define a bodyParser as middleware to populate req.body.

This can be done by users in their bolt app by adding the following code:

const express = require('express)

expressReceiver.router.use(express.json())
expressReceiver.router.post('custom/path', (req, res) => {
  // it is defined and populated
  console.log(req.body)
  // You're working with an express req and res now.
  res.send('yay!');
});

My suggestion is we make this easier for users. Two options:

Option 1 - Add this bodyParser middleware in ExpressReceiver so users don’t have to specify it. Maybe pick express.json as the default but allow users to override it.

Option 2 - Exposes the built in parsers on ExpressReceiver so users don’t have to require('express')

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • testing related
  • discussion

Requirements (place an x in each of the [ ])

  • I’ve read and understood the Contributing guidelines and have done my best effort to follow them.
  • I’ve read and agree to the Code of Conduct.
  • I’ve searched for any related issues and avoided creating a duplicate issue.

Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

package version:

node version:

OS version(s):

Steps to reproduce:

Expected result:

What you expected to happen

Actual result:

What actually happened

Attachments:

Logs, screenshots, screencast, sample project, funny gif, etc.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:17
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
kaifreshcommented, Dec 31, 2020

Hi guys, I’ve run into this problem too and actually had more luck with:

expressReceiver.router.use(bodyParser.urlencoded({ extended: true }));

Using express.json() didn’t work for me. I’m just adding this here for posterity! Also this related SO article.

2reactions
albarivascommented, Jan 7, 2022

Hi, I’m defining a custom HTTP route this way:

const salesforceMessageCallback = {
    path: '/salesforce/message',
    method: ['POST'],
    handler: salesforceMessageHandler
};

const registerCustomRoutes = () => {
    const routes = [];
    routes.push(salesforceMessageCallback);
    return routes;
};

const app = new App({
    customRoutes: registerCustomRoutes()
});

req.body arrives undefined as explained in this thread. I’ve tried the suggested solutions but I don’t get it to work. Is it needed to use a custom receiver to solve this problem?

Thanks.

Edit: I found a way to do it, please advice if it’s best practice.

const getRawBody = require('raw-body');

const salesforceMessageHandler = async (req, res) => {
    const rawBody = await getRawBody(req);
    const body = JSON.parse(rawBody.toString());
    console.log(body[0].id); // id is one of the parameters that arrives in the body
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Express.js req.body undefined - Stack Overflow
But still when I ask for req.body.something in my routes I get some error pointing out that body is undefined . Here is...
Read more >
5.x API - Express.js
As req.body 's shape is based on user-controlled input, all properties and ... You can add middleware and HTTP method routes (such as...
Read more >
Anatomy of an HTTP Transaction | Node.js
Instantiate an HTTP server with a request handler function, and have it listen on a port. Get headers, URL, method and body data...
Read more >
request.body is undefined in node js - You.com
Looks like you are hooking up your routes before any parsing middleware, which would explain why you don't get any body (usually if...
Read more >
How to extend the Express Request object in TypeScript
You might need to elaborate on the data you received in the HTTP request and provide the controllers with custom info. In JavaScript,...
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