Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean in a URI query?

Tags:

http

url

uri

What is the preferred way to specify boolean value in the query part of URI? A normal query string looks like

a=foo&b=bar 

Say I have a parameter "c" with boolean value, should I state

a=foo&b=bar&c=1 

Or

a=foo&b=bar&c=True 

Or

a=foo&b=bar&c=true 

I checked the query component section of RFC 2396 and it does not specify how to express a boolean parameter. So what I want to know is what is the common (or reasonable) way to do it?

like image 704
Clint Avatar asked Jun 23 '11 23:06

Clint


2 Answers

It completely depends on the way you read the query string. All of these that you ask for are valid.

like image 152
Petar Ivanov Avatar answered Oct 05 '22 08:10

Petar Ivanov


In node with an express server, you can add a boolean parser middleware like express-query-boolean.

var boolParser = require('express-query-boolean');  // [...]   app.use(bodyParser.json()); app.use(boolParser()); 

Without

// ?a=true&b[c]=false  console.log(req.query); // => { a: 'true', b: { c: 'false' } }  

With

// ?a=true&b[c]=false  console.log(req.query); // => { a: true, b: { c: false } }  
like image 27
nicholas Avatar answered Oct 05 '22 08:10

nicholas