Giter Site home page Giter Site logo

Comments (4)

dougwilson avatar dougwilson commented on May 2, 2024

I mean, the example works just fine. The req.body check is in there simply as a precaution, because you can never be too careful...

If the user does not set a Content-Type header at all, how do I default to application/json so I always get a populated object instead of an empty one?

This doesn't make any sense, because if there is no Content-Type, the user is 100% doing something wrong. The answer here is they need to send a content type :)

Or at the very least how do I tell them they are missing that Content-Type header? If the Content-Type is set, how do I determine that it is not correct and send them an appropriate error?

This is a problem that will be solved in 2.0, but for now, this is what you can do:

var bodyParser = require('body-parser')
var createError = require('http-errors')
var express = require('express')
var onFinished = require('on-finished')

var app = express()

// create application/json parser
var jsonParser = bodyParser.json()

// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })

// POST /login gets urlencoded bodies
app.post('/login', singularBodyType(urlencodedParser), function (req, res) {
  if (!req.body) return res.sendStatus(400)
  res.send('welcome, ' + res.body.username)
})

// POST /api/users gets JSON bodies
app.post('/api/users', singularBodyType(jsonParser), function (req, res) {
  if (!req.body) return res.sendStatus(400)
  // create user in req.body
})

function singularBodyType(parser) {
  return function parseBody(req, res, next) {
    if (!req.headers['content-type']) {
      return next(createError(415, 'missing content-type'))
    }

    parser(req, res, function (err) {
      if (err) return next(err)

      if (!onFinished.isFinished(req)) {
        return next(createError(415, 'unsupported content-type header'))
      }

      next()
    })
  }
}

from body-parser.

dougwilson avatar dougwilson commented on May 2, 2024

P.S. I have been toying with a way to be able to automatically send those kinds of 415s in the future as well. If you have an idea for a good way, feel free to send a PR :)

from body-parser.

chandara avatar chandara commented on May 2, 2024

Sorry, I am new to node and express, and I have tried the "express route-specific" with express router, but it doesn't work. Is it correct behavior? if so, is there any solution that I can use the body-parser for specific route in router?

from body-parser.

dougwilson avatar dougwilson commented on May 2, 2024

Hi @chandara , I'd be happy to help. Please open a new issue at https://github.com/expressjs/body-parser/issues/new and explain your question, including the definition of "it doesn't work" and we'll proceed from there :)

from body-parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.