有人可以向我解释一下在使用时实际上需要对在 http 请求的正文部分发送的数据进行 Url 编码吗
content-type: application/x-www-form-urlencoded
谢谢
“需要”是指“目的”吗?
如果你追求的是紫色——它只是告诉服务器会发生什么:URL 编码的键=值对。它还允许服务器知道什么不是它的方式 - multipart/form-data 之类的!这允许服务器明确地知道如何读取传入的数据。
数据作为一个标头发送(这也是它有大小限制的原因)。因此,您绝对希望避免使用以下内容:换行符、冒号。除此之外,您肯定希望在数据中转义 =,以免与 key=value 结构混淆。你也想逃避 & 出于同样的原因。URL 编码可以做到这一切——所以只有设计 HTTP 协议的人才会这样做才有意义!
There are multiple ways to send data to the server in a POST request; URL Encoded data is just one of several possible formats.
The client and server have to agree on the format of data in the POST body. URL Form encoded data is the easiest to use because of its universal support. Browsers support it natively. Every programming language allows you to read url encoded post parameters using a familiar syntax.
But of course, there is no need to use url form encoded. You can as easily send json or xml encoded POST body. As long as the client and server are in sync, you can even create a totally different encoding and use it.