Skip to content

Bodyparser returns undefined value JSON #340

@aviralgoyal

Description

@aviralgoyal

I've copied the JSON code in the documentation and sent a post request using Postman. I get a return value "undefined". I am attaching the code below, please advice. Thanks in advance.

Node Code:

// bodies of all incoming requests. This is the simplest setup.

var express = require('express')
var bodyParser = require('body-parser')

var app = express()
const port = 3000
 
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
 
// POST /login gets urlencoded bodies
app.post('/login', urlencodedParser, function (req, res) {
  if (!req.body) return res.sendStatus(400)
  res.send('welcome, ' + req.body.username)
})
 
app.listen(port, () => {
  console.log('Server started at port: ' + port)
})

POSTMAN Request code

POST /login HTTP/1.1
Host: localhost:3000
Content-Type: application/json
cache-control: no-cache
Postman-Token: 98d3bcb2-1326-4358-8d2a-91b25fbfae89
{
    "username": "aviral"
}------WebKitFormBoundary7MA4YWxkTrZu0gW--

Response:

welcome, undefined

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions