如何正确转义 POST body?
How do I escape POST body correctly?
我需要使用 POST 请求向服务器发送一些 JSON 值;发送它们而不进行任何转义工作正常,但这不是一个合适的解决方案,因为这些值可能包含特殊符号,如? &.
我试过其他问题的答案(Uri.EscapeDataString、Uri.EscapeUriString、System.Net.WebUtility.UrlEncode、System.Web.HttpUtility.UrlEncode),但它们都使服务器 return 成为 "Bad request"错误。
如何转义 POST 值 正确 ?
经过我们在评论中的简短交谈,我想我已经足够了解您的问题,可以整理出一个答案。就这样吧。
我看到有人问了三个略有不同的问题。
- 如何转义 HTTP
POST
请求的正文?
我会引用我对此的评论,因为它非常完整。
An HTTP request body (POST or otherwise) is just bytes, the length of which is determined by the Content-Length header (or something to that effect, if chunked encoding is in use). You don't have to worry about anything being escaped for that to work, escaping only comes into play at the next level up, the content type.
本质上,对于香草 POST 的任何类型的转义都没有原生要求。
application/json
和application/x-www-formurl-encoded
怎么玩?
在大多数情况下,他们就是不这样做。他们可以,而且我想我以前见过,但我真的想不出你为什么想要这样做。
仅作为背景,并确保我们在同一页面上,这是同一个对象,分别序列化为 JSON 和表单数据。
普通对象(用于比较):
PropertyName: The value is "1 & 2"
OtherProperty: 2
JSON:
{
"PropertyName" : "The value is \"1 & 2\"",
"OtherProperty" : 2
}
表单数据:
PropertyName=The%20value%20is%20%221%20%26%202%22&OtherProperty=2
如您所见,该值变得非常混乱。但是你明白了。
所以,是的,您可以相当合理地将一个嵌套在另一个中,但是没有多少用例可以说明这一点。
- 如何逃脱JSON?
如果您使用的是图书馆,该图书馆应该(如果它有价值的话)为您做这件事。 JSON.net 会,例如。我相信您唯一需要担心转义的是 JSON 中的双引号,但我不是专家,知道有边缘情况我不会感到惊讶。
我会再次引用我的评论,因为它似乎对你有所帮助。
In most modern APIs ("most" and "modern" are up for interpretation there), JSON is a pretty reasonable expectation, yeah. So then we go back to my first comment, and say that you'd only really have to worry about escaping within the JSON elements themselves, and if you're using a library, that should be handled for you. You decidedly don't want to double-encode, for obvious reasons.
The quick way of testing your entire setup, of course, is just to try and submit regular data (the control) and make sure it works, then submit something with a double-quote, and if you want, a question mark.
如果您 运行 该测试而第二个测试失败,则很有可能某些东西没有被 属性 转义。这可能是使用调试工具的好时机,或者甚至只是像 Fiddler 这样的应用程序来检查请求负载是否有任何不合理的东西,例如,如果你的 JSON 看起来像 { "name":"val"ue" }
我需要使用 POST 请求向服务器发送一些 JSON 值;发送它们而不进行任何转义工作正常,但这不是一个合适的解决方案,因为这些值可能包含特殊符号,如? &.
我试过其他问题的答案(Uri.EscapeDataString、Uri.EscapeUriString、System.Net.WebUtility.UrlEncode、System.Web.HttpUtility.UrlEncode),但它们都使服务器 return 成为 "Bad request"错误。
如何转义 POST 值 正确 ?
经过我们在评论中的简短交谈,我想我已经足够了解您的问题,可以整理出一个答案。就这样吧。
我看到有人问了三个略有不同的问题。
- 如何转义 HTTP
POST
请求的正文?
我会引用我对此的评论,因为它非常完整。
An HTTP request body (POST or otherwise) is just bytes, the length of which is determined by the Content-Length header (or something to that effect, if chunked encoding is in use). You don't have to worry about anything being escaped for that to work, escaping only comes into play at the next level up, the content type.
本质上,对于香草 POST 的任何类型的转义都没有原生要求。
application/json
和application/x-www-formurl-encoded
怎么玩?
在大多数情况下,他们就是不这样做。他们可以,而且我想我以前见过,但我真的想不出你为什么想要这样做。
仅作为背景,并确保我们在同一页面上,这是同一个对象,分别序列化为 JSON 和表单数据。
普通对象(用于比较):
PropertyName: The value is "1 & 2"
OtherProperty: 2
JSON:
{
"PropertyName" : "The value is \"1 & 2\"",
"OtherProperty" : 2
}
表单数据:
PropertyName=The%20value%20is%20%221%20%26%202%22&OtherProperty=2
如您所见,该值变得非常混乱。但是你明白了。
所以,是的,您可以相当合理地将一个嵌套在另一个中,但是没有多少用例可以说明这一点。
- 如何逃脱JSON?
如果您使用的是图书馆,该图书馆应该(如果它有价值的话)为您做这件事。 JSON.net 会,例如。我相信您唯一需要担心转义的是 JSON 中的双引号,但我不是专家,知道有边缘情况我不会感到惊讶。
我会再次引用我的评论,因为它似乎对你有所帮助。
In most modern APIs ("most" and "modern" are up for interpretation there), JSON is a pretty reasonable expectation, yeah. So then we go back to my first comment, and say that you'd only really have to worry about escaping within the JSON elements themselves, and if you're using a library, that should be handled for you. You decidedly don't want to double-encode, for obvious reasons.
The quick way of testing your entire setup, of course, is just to try and submit regular data (the control) and make sure it works, then submit something with a double-quote, and if you want, a question mark.
如果您 运行 该测试而第二个测试失败,则很有可能某些东西没有被 属性 转义。这可能是使用调试工具的好时机,或者甚至只是像 Fiddler 这样的应用程序来检查请求负载是否有任何不合理的东西,例如,如果你的 JSON 看起来像 { "name":"val"ue" }