application/json 和 application/x-www-form-urlencoded 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9870523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:14:37  来源:igfitidea点击:

differences in application/json and application/x-www-form-urlencoded

jsonweb-serviceshttp-headers

提问by Prithvi Raj Nandiwal

What is the difference between

之间有什么区别

request.ContentType = "application/json; charset=utf-8";

request.ContentType = "application/json; charset=utf-8";

and

webRequest.ContentType = "application/x-www-form-urlencoded";

webRequest.ContentType = "application/x-www-form-urlencoded";

回答by Icarus

The first case is telling the web server that you are posting JSON data as in:

第一种情况是告诉 Web 服务器您正在发布 JSON 数据,如下所示:

{ Name : 'John Smith', Age: 23}

The second option is telling the web server that you will be encoding the parameters in the URL as in:

第二个选项是告诉 Web 服务器您将对 URL 中的参数进行编码,如下所示:

Name=John+Smith&Age=23

回答by fgul

webRequest.ContentType = "application/x-www-form-urlencoded";

webRequest.ContentType = "application/x-www-form-urlencoded";

  1. Where does application/x-www-form-urlencoded's name come from?

    If you send HTTP GETrequest, you can use query parameters as follows:

    http://example.com/path/to/page?name=ferret&color=purple

    The content of the fields is encoded as a query string. The application/x-www-form- urlencoded's name come from the previous url query parameter but the query parameters is in where the body of request instead of url.

    The whole form data is sent as a long query string.The query string contains name- valuepairs separated by &character

    e.g. field1=value1&field2=value2

  2. It can be simple requestcalled simple - don't trigger a preflight check

    Simple request must have some properties. You can look herefor more info. One of them is that there are only three values allowed for Content-Type header for simple requests

    • application/x-www-form-urlencoded
    • multipart/form-data
    • text/plain
  1. 哪里应用程序/ x-WWW的形式了urlencoded的名字从何而来?

    如果您发送 HTTP GET请求,您可以使用如下查询参数:

    http://example.com/path/to/page?name=ferret&color=purple

    字段的内容被编码为查询字符串。该application/x-www-form- urlencoded的名字来自于以前的网址查询参数,但查询参数是在请求的主体中,而不是网址。

    整个表单数据作为一个长查询字符串发送。查询字符串包含由&字符分隔的名称-值

    例如 field1=value1&field2=value2

  2. 它可以是简单的请求,称为 simple -不要触发预检

    简单的请求必须有一些属性。你可以在这里查看更多信息。其中之一是对于简单请求的 Content-Type 标头只允许三个值

    • 应用程序/x-www-form-urlencoded
    • 多部分/表单数据
    • 文本/普通

3.For mostly flat param trees, application/x-www-form-urlencoded is tried and tested.

3.对于大多数扁平参数树,application/x-www-form-urlencoded 已经过尝试和测试。

request.ContentType = "application/json; charset=utf-8";

request.ContentType = "application/json; charset=utf-8";

  1. The data will be jsonformat.
  1. 数据将是json格式。

axiosand superagent, two of the more popular npm HTTP libraries, work with JSON bodies by default.

axiossuperagent是两个比较流行的 npm HTTP 库,默认使用 JSON 主体。

{
  "id": 1,
  "name": "Foo",
  "price": 123,
  "tags": [
    "Bar",
    "Eek"
  ],
  "stock": {
    "warehouse": 300,
    "retail": 20
  }
}
{
  "id": 1,
  "name": "Foo",
  "price": 123,
  "tags": [
    "Bar",
    "Eek"
  ],
  "stock": {
    "warehouse": 300,
    "retail": 20
  }
}
  1. "application/json"Content-Type is one of the Preflighted requests.
  1. “application/json”Content-Type 是 预检请求之一

Now, if the request isn't simple request, the browser automatically sends a HTTP request before the original one by OPTIONSmethod to check whether it is safe to send the original request. If itis ok, Then send actual request. You can look herefor more info.

现在,如果请求不是简单的 request,浏览器会通过OPTIONS方法自动在原始请求之前发送一个 HTTP 请求,以检查发送原始请求是否安全。如果没问题,则发送实际请求。你可以在这里查看更多信息。

  1. application/jsonis beginner-friendly. URL encoded arrays can be a nightmare!
  1. application/json 对初学者友好。URL 编码的数组可能是一场噩梦!