开发者

Boolean in a URI query?

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=开发者_StackOverflowtrue

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?


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


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


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


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.


I managed this with a custom function.

See browser compatibility here: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

function parseQuery(url = window.location.search) {
  const urlParams = new URLSearchParams(url);
  return Array.from(urlParams.keys()).reduce((acc, key) => {
    const value = urlParams.get(key);
    if (value) {
      switch (value) {
        case 'false': {
          acc[key] = false;
          break;
        }
        case 'true': {
          acc[key] = true;
          break;
        }
        case 'undefined': {
          acc[key] = undefined;
          break;
        }
        case 'null': {
          acc[key] = null;
          break;
        }
        default: {
          acc[key] = value;
        }
      }
    }
    return acc;
  }, {});
}

The function returns an object with all the parsed query parameters and fallback by default to the original value.


Or you can do this

const queryBool = unwrapTextBoolean({{{A_WAY_TO_GET_THAT_TEXT_BOOLEAN}}})
if(queryBool===null) {return;}
if(queryBool){
   /*true-based code*/
}else{
   /*false-based code*/
}

function unwrapTextBoolean(tB){
  if(tB === 'true') return true;
  if(tb === 'false') return false;
  return null;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜