Boolean in a URI query?

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


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 } } 

Use key existence for boolean parameter like ?foo

For example, use ?foo instead of ?foo=true.

I prefer this way because I don't need to waste time to think or trace the source code about whether it should be true or 1 or enable or yes or anything that beyond my imagination.

In the case of case sensitivity, should it be true or True or TRUE?

In the case of term stemming, should it be enable or enabled?

IMHO, the form of ?foo is the most elegant way to pass a boolean variable to server because there are only 2 state of it (exist or not exist), which is good for representing a boolean variable.

This is also how Elasticsearch implemented for boolean query parameter, for example:

GET _cat/master?v

Url are strings and all values in a URL are strings, all the params will be returned as strings.

it depends on how you interpret it in your code.

for the last one where c = true

you can do a JSON.parse(c)

which will change it to a boolean.

Also, you have to be careful not to pass it an empty string, if not it will throw an error.