I'm developing an API that can accept the main three data types as values to query string parameters: boolean
, string
& numeric
(integer
or float
). By default, everything is retrieved a string (I don't think there's a way around that), but the parameters are configured with a type so that I convert them to the proper type.
I now need to start accepting NULL as a value, for all three data types, but I'm not sure what is the best way to do so.
Some people seem to accept no value as NULL (?param
, without =
), but that is no quite working for me:
- I'm already accepting that as
true
for booleans, although that could be changed.
- It is received as an empty string, which is a valid value for the
string
data type.
So, here are the only two options I can see, none of which are working for me:
- Use a constant, like
"NULL"
or "~"
. That works for booleans & numbers, but not for strings, as that might be the desired string value.
- Use an empty value. Once again, that can work for booleans & numbers, but not for strings, as that might be the desired string value.
Seems like using a constant might be my only choice, and to avoid issues that constant can be forbidden from strings. But is that really the only viable option? I wish there were a better way. :(
Edit: I just had another idea that might work, but I'm not sure how it would be perceived. I think it definitely breaks standards, and a lot of people would most likely not like it, but would fix my issue better than any other solution so far. Basically, the idea would be to use JSON, which supports native data types. I'm not having this issue with POST requests because they're using JSON.
So the idea would be to take JSON in the query string, basically allowing an alternative query string that would override the standard HTTP one. Something like: ?_json={"foo":"bar","param":null}
instead of ?foo=bar¶m=~
. Or maybe even take over the query string entirely: ?{"foo":"bar","param":null}
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…