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:
create a new request
enter your API endpoint URL
select the body tab and then select the form-data tab
enter the key name of the form data you are trying to send so your API can recognize it and then the value.
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.
You’ll have to put on your detective hat on to track down how to make it work.
You could open the Postman console
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?
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.
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
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
}));
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.”
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.
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.