Giter Site home page Giter Site logo

Passing a boolean value about body-parser HOT 4 CLOSED

expressjs avatar expressjs commented on April 26, 2024
Passing a boolean value

from body-parser.

Comments (4)

dougwilson avatar dougwilson commented on April 26, 2024 8

You can 100% pass in bools using JSON with this library. Here is an example:

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

var app = express()

app.post('/', bodyParser.json(), function (req, res) {
  res.type('text')
  res.write('request posted the following:\n')
  res.write(util.inspect(req.body) + '\n')

  res.write('and the archive property is a ' + typeof req.body.archive + '\n')
  res.end()
})

app.listen(3000)

Then make a JSON post:

curl -XPOST -H'Content-Type: application/json' -d'{"archive":false,"foo":"bar"}' http://localhost:3000/
request posted the following:
{ archive: false, foo: 'bar' }
and the archive property is a boolean

Most likely you are not actually sending JSON like you think you are (you can verify using HTTP sniffing like Fiddler or something similar) or you are sending JSON, but something on your client is changing the boolean to a string and it's being sent over the wire as {"archive":"false"} (you can also verify this using something like Fiddler).

from body-parser.

dougwilson avatar dougwilson commented on April 26, 2024 4

Hi @qwemaze, I'm not sure what the comment you made means. This is body-parser, which only parses request bodies, thus it of course wouldn't parse req.query, that would be non-sense. The urlencoded format is parsed by the qs module, so you can always bring up the issue there and whatever changes they make we'll pull in here.

from body-parser.

efkan avatar efkan commented on April 26, 2024

I should to learn a lot of things :)
I've learned today that;
Nobody passes a bool or numeric value to Node.js using REST API. Because it works with strings.
Therefore, there are lots of ODM (Object Document Mapper) tools.

Hopefully you will say "you're wrong. You can".. Otherwise, I have to re-write my controllers using Mangoose :)

I'm closing the issue. Thanks again..

from body-parser.

qwelias avatar qwelias commented on April 26, 2024

@dougwilson yeah, but it'll only work with PUT/POST requests, because bodyParser.json doesn't parse req.query, and bodyParser.urlencoded doesn't seem to parse numbers/booleans.

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.