'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 1:[1]

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.

Solution 2:[2]

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());

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Carbonhell
Solution 2 Sehrish Waheed
Solution 3 Marcel Stucki