POSTMAN not sending anything in the body in POST requests
Asked Answered
M

4

8

I am trying to test the /login API via POSTMAN (via FE it works fine) but it doesn't show anything in the body part even though I am sending body data.

enter image description here

but when printing the request from the BE, the body is empty...

  ....
  body: {},
  ....

unlike when using FE:

  ....
  body: {data: { username: 'admin', password: 'admin' }},
  ....

Any idea what's going on? If anything else is needed to be provided - pls let me know

I know it's going through because the server responds with 500 and the message

TypeError: Cannot read property 'username' of undefined

The weird part is, that the data I am sending, are nowhere to be found in the request object at all :(

EDIT:

This is how I call it from the FE:

return axios.post('login', { data: user })

and the user is:

user: {
  username: 'admin',
  password: 'admin'
}

So the format should be right

data: { 
    username: 'admin', 
    password: 'admin'
}

Because that's how I access it on the BE side

req.body.data.username

EDIT2:

The ultra-super-rare-weird part is, that JEST is working fine :)

const creds = {
      data: {
            username: 'admin',
            password: 'admin'
            }
    }

    return request(app)
      .post("/api/v1/login")
      .send(creds)
      .expect(200)
      .then(res => {
        expect(res.body).toMatchSnapshot()
      })

and this test passes .... f**k me guys.. what's going on?

Maryrosemarys answered 10/4, 2021 at 14:0 Comment(0)
L
12

The syntax of your body looks like JSON, yet you've specified the type of the body as "raw text". This will set the Content-type header of your request to "text/plain", which is probably causing your backend to be unable to actually read the body (because it expects a JSON object).

Simply switch from "Text" to "JSON", wrap your current body in curly braces (so that you're actually sending a single JSON object with a data property set) and try sending the request again. The content-type header will be correctly set to "application/json" this time and your backend will read the data successfully.

Lining answered 10/4, 2021 at 14:12 Comment(7)
I tried that too of course.. but no luck either .... SyntaxError: Unexpected token d in JSON at position 6 ...Maryrosemarys
Ah sorry, you also need to wrap the word "data" in double quotes, otherwise it will be read as a token (instead of a string)Lining
I did :) { data: { "username": "admin", "password": "admin" } }Maryrosemarys
Your example doesn't show the double quotes around data, it should be like this: { "data": { "username": "admin", "password": "admin" } }. Does it give any other error with this body?Lining
it's still the same... I tried with, without, single, double, ... everything ends up with the same error Cannot read .... ... but how come the data I am sending is not in the request at all? I mean.. I wouldn't care much about them not being in the body.. but they are nowhere!Maryrosemarys
weird.. solved by admin the double quotes again and tagging it as json (instead of raw).. .dunno why it didn't work before... anyway, tagged your reply as correct answer ! thxMaryrosemarys
Postman is completely broken for me. The body is well-formed JSON. There's a Content-Type: application/json request header. It looks like a dodgy Postman update which has completely broken its ability to send a request body. Great quality control.Gileadite
B
13

if you are working with an express server try to parse your body in the server as soon as you initialize express app before routing

const app = express();
app.use(express.json());
Belamy answered 7/10, 2021 at 7:42 Comment(1)
lifesaver, save me a lot of troubleSigridsigsmond
L
12

The syntax of your body looks like JSON, yet you've specified the type of the body as "raw text". This will set the Content-type header of your request to "text/plain", which is probably causing your backend to be unable to actually read the body (because it expects a JSON object).

Simply switch from "Text" to "JSON", wrap your current body in curly braces (so that you're actually sending a single JSON object with a data property set) and try sending the request again. The content-type header will be correctly set to "application/json" this time and your backend will read the data successfully.

Lining answered 10/4, 2021 at 14:12 Comment(7)
I tried that too of course.. but no luck either .... SyntaxError: Unexpected token d in JSON at position 6 ...Maryrosemarys
Ah sorry, you also need to wrap the word "data" in double quotes, otherwise it will be read as a token (instead of a string)Lining
I did :) { data: { "username": "admin", "password": "admin" } }Maryrosemarys
Your example doesn't show the double quotes around data, it should be like this: { "data": { "username": "admin", "password": "admin" } }. Does it give any other error with this body?Lining
it's still the same... I tried with, without, single, double, ... everything ends up with the same error Cannot read .... ... but how come the data I am sending is not in the request at all? I mean.. I wouldn't care much about them not being in the body.. but they are nowhere!Maryrosemarys
weird.. solved by admin the double quotes again and tagging it as json (instead of raw).. .dunno why it didn't work before... anyway, tagged your reply as correct answer ! thxMaryrosemarys
Postman is completely broken for me. The body is well-formed JSON. There's a Content-Type: application/json request header. It looks like a dodgy Postman update which has completely broken its ability to send a request body. Great quality control.Gileadite
N
6

Add the following parameters in the request headers configuration in Postman:

 Content-Type: application/json
 Content-Length

The value of Content-Length is automatically calculated by Postman when a request is sent. Both values are used to identify the media type of the request body and to parse it accurately. When missing, the body may be ignored completely (depending on the server).

Nazarite answered 25/4, 2022 at 8:24 Comment(0)
D
0

You should use

app.use(express.json());

try logging body data on console after doing this

console.log(req.body);
Douro answered 17/6 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.