fetch() POST request to Express.js generates empty body {} fetch() POST request to Express.js generates empty body {} express express

fetch() POST request to Express.js generates empty body {}


Add the following line in addition to your current bodyParser app.use() right before:

app.use(bodyParser.json());

This will enable bodyParser to parse content type of application/json.

Hopefully that helps!


I feel a bit stupid now once I worked out what bodyParser.urlencoded (source: https://www.npmjs.com/package/body-parser#bodyparserurlencodedoptions)

Returns middleware that only parses urlencoded bodies

Solution

Changed the fetch() request, from:

body: "MY DATA",headers: { "Content-Type": "application/json" }

to

body: JSON.stringify({"Data": "MY DATA"}),headers: { "Content-Type": "application/x-www-form-urlencoded" }

which solved the problem! Console logged:

{ '{"Data":"MY DATA"}': '' }