Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should REST API accept boolean values?

Tags:

rest

http

url

Resource 123 has a current configuration state and a default configuration state, and both of these configuration states can be represented by JSON.

A GET request to http://example.com/123/config will return the current configuration state, and a GET request to http://example.com/123/config?reset=true will return the default configuration state.

How should an API interpret boolean values? For instance:

  • http://example.com/123/config?reset=true
  • http://example.com/123/config?reset=blablabla
  • http://example.com/123/config?reset=false
  • http://example.com/123/config?reset=1
  • http://example.com/123/config?reset=0
  • http://example.com/123/config?reset=
  • http://example.com/123/config?reset
like image 669
user1032531 Avatar asked Oct 11 '16 14:10

user1032531


People also ask

Why you shouldn't use booleans in rest APIS?

If not carefully considered, booleans can: Obstruct API Extensibility. Mask and obfuscate Domain Clarity. Hamper Code Readability and Maintainability.

Should Booleans be quoted in JSON?

When outputting to YAML or JSON, boolean values should not have quotes around them. An output file will produce E3012 errors when ran through cfn-lint because of this.

How can the postman pass Boolean values?

So even if you send a parameter like “active=true”, it is still a string, this is how the HTTP protocol works. Hope this helps clarify the mystery. var bflag = Boolean(“true”); var bflag1 = Boolean(“false”);


2 Answers

True and false

The true and false literals are just fine to represent boolean values. They are quite descriptive and, if your API supports JSON, true and false are definitively the obvious choices.

Enumerations

In a few situations, however, you may want to avoid boolean values because they cannot be expanded. You may want to consider enumerations instead.

It may be a poor comparison but it might help you to get the main idea of this approach: have a look at CSS properties such as overflow or visibility. They allow expandable values instead of only true or false. So new values can be easily added without changing the property names.

So, for the situation described in your question, to retrieve the default state of a resource, I would support a query parameter such as status, that could have values such as default and current.

The following would return the default state of the resource:

GET /config?status=default HTTP/1.1
Host: example.com
Accept: application/json

And the following would return the current state of the resource:

GET /config?status=current HTTP/1.1
Host: example.com
Accept: application/json

If no query parameter is provided, you could that the client wants the current state of the resource.


If you need to restore the resource state to its default state, consider using PUT, sending the new representation of the resource in the request payload. Something like:

PUT /config/status HTTP/1.1
Host: example.com
Content-Type: application/json

{ 
  "value": "default" 
}
like image 176
cassiomolin Avatar answered Sep 23 '22 21:09

cassiomolin


Whichever way you want it to, it's completely up to you as the architect/designer. true/false is the most syntactically correct version, make sure that one works and add the other options as sugar if you want.

like image 26
rorschach Avatar answered Sep 24 '22 21:09

rorschach