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

Goal: send some defined string data from HTML in a fetch() function e.g. “MY DATA”

My code:


<!DOCTYPE html>

<script type="text/javascript">
    function fetcher() {
          method: "POST",
          body: "MY DATA",
          headers: {
            "Content-Type": "application/json"
      .then(function(response) {
        return response.json();
      .then(function(myJson) {



var express = require("express");
var app     = express();
var compute = require("./compute");
var bodyParser = require("body-parser");

//not sure what "extended: false" is for
app.use(bodyParser.urlencoded({ extended: false }));

app.post('/compute', (req, res, next) => {
    var result = compute.myfunction(req.body);

Currently: console.log(req.body) logs {}

Desired: console.log(req.body) logs "MY DATA"


  1. I also tried sending body in fetch() as body: JSON.stringify({"Data": "MY DATA"}) but get the same empty {}
  2. I either my fetch() request, or my bodyParser(), is not setup correctly.

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


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


Changed the fetch() request, from:

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


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

which solved the problem! Console logged:

{ ‘{“Data”:”MY DATA”}’: ” }

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   What is the best way to test for an empty string with jquery-out-of-the-box?

Similar Posts