postmanhttp-post

POSTMAN not sending anything in the body in POST requests


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?


Solution

  • 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.