Custom http routes don't currently include req.body
See original GitHub issueDescription
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:
- Created 3 years ago
- Reactions:17
- Comments:10 (3 by maintainers)
Hi guys, I’ve run into this problem too and actually had more luck with:
Using
express.json()
didn’t work for me. I’m just adding this here for posterity! Also this related SO article.Hi, I’m defining a custom HTTP route this way:
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.