Web development and Tech news Blog site. WEBISFREE.com

HOME > webdevetc

[NodeJS] Resolving the issue of not being able to retrieve the request body in nodejs express when making post and patch requests through ajax.

Last Modified : 27 Feb, 2023 / Created : 27 Feb, 2023
723
View Count
Using axios in the front-end app, we passed parameters in the body and sent them to the server. There were no special settings and it was very simple. Naturally, I thought it would work well, but unfortunately, I couldn't get any values.




# Unable to retrieve the body in the Express Node server


Is it being passed somewhere else other than the body? Is it a method problem? Even after changing it to get, post, or patch and searching for the query, params, and body of the request, only empty values were passed. It's a strange problem where it's only passed well as res.query when passed through the query string.


! Searching for a solution after confirming the issue


What could be the problem? I found many documents, but most of them were examples of ajax, axios settings, or nodejs express. I tried various attempts, but even changing the body format or encoding setting in the header resulted in the same empty values.

I couldn't think of any reason why it wouldn't work... After searching a bit more, I finally identified the cause on a specific search page. The cause was surprisingly simple; the function for parsing the body in nodejs express was not there, so it couldn't pass the value. It was a part that I had never thought of.


! Solution for express body parsing


The simplest solution is to install body parsing. Installing npm's body-parser is the easiest and simplest way. After installation, set it up as follows:
npm install body-parser

Once the installation is complete, set bodyParser in express as follows:
const express = require('express')
const bodyParser = require('body-parser')

const app = express()
app.use(bodyParser.json())

All the steps are complete. The above code is for content-type application/json. If it's application/x-www-form-urlencoded, add or change the setting value as follows.
// application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

After making the changes as shown above, we need to test it out. Let's create a simple example and request the server through ajax in the Front app. The example code is as follows:
const options = {
  headers: {
    'Content-type': 'application/json; charset=UTF-8'
  }
}

try {
  const result = await axios.post(url, { title: 'abc' }, options)
  // Success case
} catch(e) {
  // Error handling
}

We ran the above code and checked the results on the console in NodeJS Express. It worked well as expected.
router.use('/test', function(req, res) {
  console.log(req.body)
})

// Result
{
  title: 'abc'
}

Fortunately, we were able to solve it easily. We were lucky to identify the cause quickly, but if we had identified the cause late, it would have been a very troublesome issue.

So far, this was the solution for the body parameter issue in NodeJS Express.
Perhaps you're looking for the following text as well?

    Previous

    [WSL] How to reboot and restart Linux on Windows

    Previous

    Changing Node and NPM version using nvm.