This is my Node Express Code,
(function () {
\'use strict\';
var fs = require(\'fs\');
var cors = require(\'cors\');
var bodyParser = requir
This normally happens because of the "Content-type" header in your http request.
JSON Bodies
bodyParser.json() only accepts the type of "application/json" and rejects other non-matching content types.
Solutions 1: Accepting Extra Content Types Other Than application/json
check your request header "Content-type" and add it to options argument in bodyParser.json([options]) in server.
for example if you have this:
"Content-type: application/csp-report"
app.use(bodyParser.json({ type: ["application/json", "application/csp-report"] }));
you can also use the built-in middleware in exprss v4.16.0 onwards:
app.use(express.json({ type: ['application/json', 'application/csp-report'] }));
for more information see this documentation
Notice: use this approach only where you can not change Content-type
to application/json
.
Solutions 2 ( recommended ): Change Header content-type
To application/json
In Http Request.
What about urlencoded bodies ?
This is similar to json bodies with two differences:
"Content-type" header accepted in request is "application/x-www-form-urlencoded"
body payload is a url encoded format (Not a json object):
Format: param_1=value_1¶m_2=value_2&...
app.use(express.urlencoded({ extended: false }));
see docs.