The POST request is returning a NULL value for req.body {}

https://i.sstatic.net/v72zp.png

When sending a post request from POSTMAN, req.body contains the data but when submitting the form it returns an empty object {}.

Initially, I used the following middleware:

app.use(bodyParser.json());

app.use(bodyParser.urlencoded({ extended: false}));

However, I received a deprecation warning for 'bodyParser' indicating that it is outdated. Additionally, a deprecated message was shown.

I attempted to replace it with:

app.use(express.json());

app.use(express.urlencoded({ extended: false}));

Despite this change, the issue still persists with req.body returning an empty object.

https://i.sstatic.net/v72zp.png

Answer №1

Make sure to pay attention to the order in which you register dependencies in your code. For example, if you are registering express json, it should come before your routes. Here's an example:

const express = require('express');
const app = express();

// register middleware here
app.use(express.json());

// define your route
app.post('/someroute', async (req,res,next) => {
  const { param1, param2 } = req.body;
  try {
    console.log(`${param1} and ${param2} printed in the terminal);
    res.send('ok');
  } catch (err) {
    res.send('oopps');
  }
})

Answer №2

Are you confident that you are setting the appropriate headers when making your POST request?

If you are parsing for .json(), your headers must include: Content-Type: application/json

If you are parsing form data with urlencoded(), your headers should include:

Content-Type: application/x-www-form-urlencoded

Could you also provide the CURL command from your Postman request?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a potential issue with spreading on IE 11 in (Webpack, VueJS)?

Encountering an error on Internet Explorer 11: https://i.sstatic.net/zGEeK.png https://i.sstatic.net/FKSUs.png Reviewing my webpack and babel configurations: webpack configuration const path = require('path'); const webpack = require('we ...

Navigating a plethora of unpredictable identifiers using AngularJS

My goal is to display content from a json file that varies in type and consists of pages divided into chapters. Users can navigate through the content using an aside menu or next and previous buttons. Currently, I am able to display the content by inserti ...

Submitting with enter key on typeahead suggestions

Seeking guidance on Bootstrap typeahead: how can I configure it to submit the entered text upon pressing the enter key? Specifically, if I type "hello" in the typeahead input and hit enter, how can I submit "hello"? I've successfully disabled the aut ...

Having difficulty removing database entries using fetch and express in my React application

0 I recently developed a MERN app and utilized the Fetch API for data manipulation. I encountered an issue where I am unable to delete specific data upon clicking, even though I have provided the endpoint for deletion. Every time I attempt to delete, the ...

Tips for emphasizing the currently pressed button element when clicked in Angular 2?

Here is the code snippet I am currently working with: <button *ngFor="let group of groupsList" attr.data-index="{{ group.index }}" (click)="processGroups(group.index)">{{ group.title }}</button> I am trying to figure out if it is possible to ...

Personalizing the React Bootstrap date picker

I am currently working on customizing the react-bootstrap-daterangepicker to achieve a specific look: My goal is to have distinct background colors for when dates are selected within a range and when the user is hovering over dates to make a selection. I ...

Troubleshooting Type Conversion Error in ASP.NET MVC Controller

I have been working on an application that utilizes the following HTML and JavaScript. The user is required to input 5 props and then click on the 'Create' button. Subsequently, the JavaScript code compiles all of these props into a list before s ...

Can a webpage be customized to display different content based on whether the visitor is a web crawler or a web application user?

As I work on setting up my web page to support server-side rendering (SSR), a question arises: Is there a way for me to determine if the client visiting my site is a web crawler? If possible, I would like to serve my web page as it is to clients who are n ...

Combining two THREE.js scenes without erasing the first one

I'm facing an issue where I am attempting to overlay two scenes in my rendering process, but the second scene is completely removing the content of the first scene. Here is how my renderer is set up: this.renderer = new THREE.WebGLRenderer({ antia ...

Error 404 encountered while attempting to access dist/js/login in Node.JS bootstrap

I currently have a web application running on my local machine. Within an HTML file, I am referencing a script src as /node_modules/bootstrap/dist/js/bootstrap.js. I have configured a route in my Routes.js file to handle this request and serve the appropri ...

Incorporating Nunjucks into Express 4

I recently attempted to integrate Nunjucks as my template engine within my Express application. Here's the code snippet I used: var express = require('express'); var nunjucks = require('nunjucks'); var path = require('path&ap ...

Text alignment issues cause animation to vanish

Utilizing particles.js, I set up a full-screen particle effect by specifying the animation to be full-screen with height: 100vh;. This worked perfectly as intended. However, when attempting to add text on top of the particle animation and center it vertica ...

The process of transferring a file from a client to a server and then from one server to another

Within my web application, I am currently working on implementing a file upload feature. I have successfully utilized axios to send file requests from the client to the server's API, including proper form data appending and necessary headers. However, ...

Trouble encountered when integrating npm library with webpack

Recently, I set up a webpack project with vuejs using the template from https://github.com/vuejs-templates/webpack-simple. My intention was to integrate https://www.npmjs.com/package/bravia into my vue application by importing it with import Bravia from &a ...

The scrolltop animation does not appear to be functioning properly on iOS devices

I have implemented a JavaScript effect to increase the line-height of each list item on my website as you scroll. It works perfectly on my Macbook and Android Smartphone, but for some reason, it's not working on an iPhone. Can anyone provide a solutio ...

Looking for assistance in reducing the vertical spacing between divs within a grid layout

Currently, I am in the process of developing a fluid grid section to showcase events. To ensure responsiveness on varying screen resolutions, I have incorporated media queries that adjust the size of the div elements accordingly. When all the divs are unif ...

Node - Creating personalized error handling functions

Currently in the process of developing custom helper methods to eliminate redundancies, utilizing express-promise-router app.js has set up the error handler middleware //errorHandler app.use((err, req, res, next) => { //const error = a ...

What is the best way to create a full-width gallery display?

I've been having some trouble making my image gallery full-width at any resolution. Oddly enough, the gallery only goes full screen when I click on the maximize button in the browser window. I can't seem to figure out what the issue is so far. ...

Warning: Update state in React-router-redux after redirection

I am currently developing an admin app for a project using react, redux, react-router, and react-router-redux. The version of react-router I am working with is v4.0.0, while react-router-redux is at v5.0.0-alpha.3 (installed with npm install react-router-r ...

Utilize CLI search to convert Splunk index data into JSON format

Currently, I have a splunk container operating within docker. My goal is to convert the raw splunk index data into json format by utilizing a CLI search and then saving the resulting output as a file on my local system. Can anyone provide guidance on how t ...