Tuesday, 11 April 2017

Express Body Parser with Custom Header

I am in the middle of setting up a REST API that takes requests with a custom Content-Type, but I am having trouble parsing the body with the NPM package body-parser. I am running some tests with Mocha and Chai-HTTP and seding my request tests like this:

chai.request(server)
.post('/demo')
.set('Content-Type', 'application/vnd+companyName.v01+json')
.send({name: 'test'})
.end(function(err, res) {/* tests are here */});

In my express app's app.js, I am calling this middleware:

app.use(bodyParser.json({type: 'application/*+json'}));

When I make the type more general, like making it 'application/*' or /, I can pass the request through with a 'application/json' Content-Type, but not my custom one. When I do this, my req.body is an empty object. If bodyParser was just completely not working, req.body would be undefined and not an empty object. By looking at the docs, I feel like my options on my bodyParser call are correct, but clearly not - any insight?



via lgh

No comments:

Post a Comment