Trying to send form data but i'm getting req.body as undefined

Describe the bug
I am trying to send form data to my API but req.body is undefined for some reason.

To Reproduce
Steps to reproduce the behavior:

  1. create a new request
  2. enter your API endpoint URL
  3. select the body tab and then select the form-data tab
  4. enter the key name of the form data you are trying to send so your API can recognize it and then the value.
  5. Click send and you should get a response with a status code of 200. If you get an error like me telling me that req.body is undefined then you have the same problem as me.

Expected behavior
I expect my API route handler function to create a new product for my ecommerce site. But instead I get an error coming from my ORM which is Sequelize. Telling me that name cannot be null because I am trying to add it to the database table. I even console.logged it on my server (im using node.js) and it req.body really is undefined.

Screenshots

Here is another picture that is showing that req.body is actually empty. I console.logged it:


App information (please complete the following information):

  • Ecommerce API (Node.js)
  • Postman Version 7.2.1
  • OS: Windows

Additional context
If you have any other question or I am forgetting something to show you guys, just let me know.

Yeah that looks frustrating @asaadullamikabir :frowning:

Welcome to the Postman community though! :+1:

You’ll have to put on your detective hat on to track down how to make it work.

You could open the Postman console
image
and run the request again. Then check how the request looks there ( try both the pretty and raw views)
That would verify if Postman thinks it’s sending the form data.

Also, is there a system that currently makes the call (web UI, mobile app etc) that you could capture using something like the browser dev tools, Fiddler or CharlesProxy etc? Then double check if you’re doing exactly the same thing in your Postman request.

I notice that the error response in your screenshot reports that Product.name cannot be null, have you tried changing the key to exactly that?

same issue ,this is real frustrating…

my body of form-data value showing null after i pass the leadId : 12345 error after testing your leadId is null

This is quite a tricky question to answer without any implementation details of the API. For me, Postman is not really the cause of the issue, it’s just showing you that there is an issue.

What framework is being used? Express or something else?

I’ve found issues with getting that data in the past using Express due to things like the body-parser module not being installed and used correctly.

2 Likes

Try using this on server side:

request.on(‘data’, (data: any) => {
console.log(data.toString());
});

request.on(‘end’, () => {
response.json({
fileUpload: ‘successful’
});
});

I am using Express and i get the same problem . I want to send some text and a file for a sign up page. I am using formidable to upload the file . The file gets uploaded. As I try to acces the body of the request it returns an empty object {}. Please Help me

Hey!
I was facing the same issue.

var bodyParser = require(‘body-parser’);
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

The above code solved the issue for me!

2 Likes

Your comment solved my problem! I did not insert my code app.use(express.json()) or body parser! Thanks!

2 Likes

I had same issue really frustrating! I used body parser as const express = require('express) → const app = express() → app.use(express.json()); It should work

app.use(express.json());

app.use(express.urlencoded({

extended: true

}));
I tried using this but didn’t worked, postman is sending the respond but on submitting the form its returning a null body.
Even body parser isn’t working:- “The declaration was marked as deprecated here.”

1 Like

I’m running into the same issue.
The problem is that postman is not sending the files as part of the from-data request body so the needed files and other multipart data is not even being sent to my API.

1 Like

Remains undefined in post man
–>Here are your undefined undefined tacos
even I add app.use(express.json()); in index.js file.

Till I figured out I didn’t change TEXT to JSON, now it works

4 Likes

This resolved the error I have been trying to fix for some hours.

Much thanks

1 Like

I was trying to upload files but was getting this error.
Sending with JSON works fine no errors there.
However even if the data I am sending over is not a file, I would get an empty object for the req.body.
The fix was to use express-fileupload middleware which would handle file uploads.

1 Like

Yeah. Even the multer middleware will fix the issue