The difference between Accept and Content-Type in HTTP
Accept and Content-Type are often used in HTTP Content. What are the differences and connections between the two?
- Different types of
Different types Accept belongs to the request header and Content-Type belongs to the entity header. Http headers are divided into generic headers, request headers, response headers, and entity headers.
The requester HTTP header structure: general header | | entity header responder HTTP request header header structure: general header header 2 | | response header entities. Effect of different
Accept represents the type of data that the sender (client) wants to Accept. For example: Accept: text/ XML; Represents the data type that the client wants to accept as XML.
The content-type on behalf of the sender (client) | server sends the data Type of the entity data. For example, content-type: text/ HTML; The data sent on behalf of the sender is in HTML format.
Accept:text/ XML; Content-type :text/ HTML: indicates that the data Type to be accepted is XML and the data to be sent in this request is HTML.
content-type application/x-www-form-urlencoded & application/json
When data is transmitted from the front end to the back end, if it is transmitted by GET, it is directly transmitted after the URL. If it is a POST transfer, it is transmitted in the request body.
There are two data formats in the body, one is json data format and the other is a string. Which format you use depends on the format of the back-end entry parameter.
{‘ Content-Type ‘:’ application/json ‘}} {‘ name ‘:’ Edward ‘, ‘age’ : ’25’}}
{‘ Content-type ‘:’ application/x-www-form-urlencoded ‘} if the backend receives a string type, headers for post should be set to {‘ Content-type ‘:’ application/x-www-form-urlencoded ‘},
, the data transmitted to the back end is like ‘name=edward&age=25’
In the development of the project, the back-end GET request needs to accept (form) string type, but the POST request needs to take over application/ JSON, and the back-end return type is uncertain, which can be a string, or json, axios configuration pit
Headers content-type does not take effect if data is not set in axios. ‘Accept’:’/’ is required.
let config; If (method==="GET"){config = {method: 'GET ', timeout: 120000, {}, withCredentials: false, headers: { 'Accept':'*/*', 'Content-Type': 'Application /x-www-form-urlencoded'}}}else{config = {method: method, timeout: 120000, url, data: method === "POST" || method === "PUT" ? params : null, withCredentials: false, headers: { // 'Content-Type': 'application/x-www-form-urlencoded' "Content-Type": "application/json" } } }Copy the code