如何在HTTP查询字符串中发送NULL? [英] How to send NULL in HTTP query string?

查看:162
本文介绍了如何在HTTP查询字符串中发送NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个API,它可以接受主要的三种数据类型作为查询字符串参数的值: boolean string & 数字整数 float )。默认情况下,一切都被检索到一个字符串(我认为没有办法解决这个问题),但参数配置了一个类型,以便我将它们转换为正确的类型。

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.

我现在需要开始接受所有三种数据类型的NULL作为值,但我不确定这样做的最佳方法是什么。

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.

有些人似乎没有接受任何值为NULL(?param ,没有 = ),但这不是很有效对我来说:

Some people seem to accept no value as NULL (?param, without =), but that is no quite working for me:


  1. 虽然我已经接受了对于布尔值的 true ,尽管可以更改。

  2. 它是一个空字符串,它是字符串数据类型的有效值。

  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. 使用常量,如NULL。这适用于布尔和&数字,但不是字符串,因为它可能是所需的字符串值。

  2. 使用空值。再一次,这对布尔人来说是可行的。数字,但不是字符串,因为它可能是所需的字符串值。

  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. :(

编辑 :我刚才有另一个想法可行,但我不确定它是怎么回事我认为这肯定会破坏标准,而且很多人很可能不会喜欢它,但是到目前为止我会比任何其他解决方案更好地解决我的问题。基本上,我们的想法是使用JSON,它支持原生数据我没有遇到POST请求的问题,因为他们正在使用JSON。

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.

所以想法是在查询字符串中使用JSON,基本上允许替代查询字符串,它将覆盖标准的HTTP。例如:?_ json = {foo:bar,param:null} 而不是?foo = bar& param =〜。或者甚至可能完全接管查询字符串:?{foo:bar,param:null }

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

推荐答案

发送URLEncoded NUL值(%00)具体设置为null。应该正确URL解码。

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

这篇关于如何在HTTP查询字符串中发送NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆