application/x-www-form-urlencoded
application/json
application/json
对初学者友好
application/x-www-form-urlencoded
对 Postman 友好
axios 和 superagent 默认使用 JSON body
来自专家的建议
The Stripe API is organized around REST. Our API has predictable resource-oriented URLs, accepts form-encoded request bodies, returns JSON-encoded responses, and uses standard HTTP response codes, authentication, and verbs
-- Stripe Docs
Creating or updating a resource involves performing an HTTP PUT or HTTP POST to a resource URI. In the PUT or POST, you represent the properties of the object you wish to update as form urlencoded key/value pairs. Don't worry, this is already the way browsers encode POSTs by default. But be sure to set the HTTP Content-Type header to "application/x-www-form-urlencoded" for your requests if you are writing your own client.
-- Twilio Docs
一些重要的事项
json 会造成预检请求问题(CORS)
Preflight OPTIONS requests are always sent with JSON
URL 编码数组是一个麻烦事
URL ENCODE: partners[]=Apple&partners[]=Microsoft&partners[]=Activision
JSON ENCODE: {"partners":["Apple","Microsoft","Activision"]}
一名开发者的回复
Ali Sherief
•[20年7月5日 • Edited on 7月5日](https://dev.to/bcanseco/request-body-encoding-json-x-www-form-urlencoded-ad9#comment-11d5i)
My app was using application/json to make requests to my API but I found a downside to it. The preflighted OPTIONS request is sent in order, but the actual POST/GET/whatever request is sent arbitrarily later after the next requests have been made. So this wrecks a stateful API if you send a POST with application/json before navigating to another page and GETing the same application/json there because the OPTIONS request for the POST will be sent first, then the next GET, then the POST itself.
For this reason I try to use application/x-www-form-urlencoded as much as possible. Preflighted requests can make race conditions.
我的应用使用 application/json
,但是发现了一些问题,预检请求按顺序发送,但是实际的 POST/GET/... 请求会任意的顺序发送