What is the best way to pass boolean parameters to a WCF RESTful service?
Using true
or false
like this:
api/resource?hasProperty=true
or using 0
or 1
like this:
api/resource?hasProperty=1
JSON BooleansValues in JSON can be true/false.
If not carefully considered, booleans can: Obstruct API Extensibility. Mask and obfuscate Domain Clarity. Hamper Code Readability and Maintainability.
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”);
You can set the data type of your parameter to [bool] to pass Boolean values to a PowerShell script from a command prompt. Copy param([int]$a, [bool]$b) switch($b){ $true {"It is true."} $false {"It is false."} } Boolean parameters accept only Boolean values and numbers, such as $True , $False , 1 or 0 .
I like the
api/resource?hasProperty=true
approach because it is more user-friendly.
It tells the API consumer indirectly that it is a boolean parameter. 0 or 1 does not imply that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With