我一直在阅读关于HTTP
标头和Accept
的内容,从我所阅读的内容来看,它是后端服务器用来了解请求客户端期望的响应类型的标头。我很好奇它与Axios请求的responseType
选项有什么关系,或者它有任何关系。
// `responseType` indicates the type of data that the server will respond with
// options are: 'arraybuffer', 'document', 'json', 'text', 'stream'
假设我向某些API发送GET
请求,我知道该API将使用JSON
主体进行响应,然后我将responseType
设置为arraybuffer
,Axios是否处理该转换?
axios
.get("https://jsonplaceholder.typicode.com/todos/1", {
headers: { Accept: "application/json" },
responseType: "arraybuffer",
})
.then((res) => {
console.log(res.data);
})
.catch((err) => {
console.log(err);
});
从代码来看,responseType
只是决定如何解析(或不解析(响应体,它与任何标头无关。
实际的响应类型由Content-Type
响应报头表示。Accept
请求标头应由服务器评估,以便根据客户端的偏好从资源的多种内容类型中进行选择(如果可用!(。有关详细信息,请参阅内容协商。