HTTP Content Type 和 Accept 的含义

HTTP About 1,025 words

Content Type

Content-Type是客户端告诉服务器,客户端携带的数据是什么格式的。

示例一

意思是告诉服务器,请求体中的文本是json格式。

POST http://localhost:8080/test1
Content-Type: application/json

{
  "variables": [
    {
      "name": "assignee",
      "type": "string",
      "value": "王五"
    }
  ]
}

示例一

POST请求,意思是告诉服务器,请求体中的文本是form表单格式。

POST http://localhost:8080/test2
Content-Type: application/x-www-form-urlencoded

variable1=aaa&variable2=bbb

Accept

Accept是客户端告诉服务器,服务器返回的数据是什么格式的。

示例一

意思是客户端告诉服务器,客户端需要的返回数据是json格式。

POST http://localhost:8080/test1
Accept: application/json
Content-Type: application/json

{
  "variables": [
    {
      "name": "assignee",
      "type": "string",
      "value": "王五"
    }
  ]
}

示例二

意思是客户端告诉服务器,客户端需要的返回数据是任意格式,服务端自己决定。

GET http://localhost:8080/test1
Accept: */*

示例二

意思是客户端告诉服务器,客户端需要的返回数据优先是json格式,其次是text文本格式,都无法生成json/text格式则由服务端自己决定。

application/json, text/plain, */*

参考

https://www.rfc-editor.org/rfc/rfc7231#section-5.3.2

https://stackoverflow.com/questions/35722586/http-headers-accept-and-content-type-in-a-rest-context

Views: 164 · Posted: 2024-01-30

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh