Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

standards - How to send NULL in HTTP query string?

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:

  1. I'm already accepting that as true for booleans, although that could be changed.
  2. 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:

  1. Use a constant, like "NULL" or "~". That works for booleans & numbers, but not for strings, as that might be the desired string value.
  2. 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&param=~. Or maybe even take over the query string entirely: ?{"foo":"bar","param":null}.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Send a URLEncoded NULL value (%00) for any thing that you're specifically setting to null. It should be correctly URL Decoded.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...