Axios是处理客户端中响应主体到responseType的转换,还是由服务器处理



我一直在阅读关于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请求标头应由服务器评估,以便根据客户端的偏好从资源的多种内容类型中进行选择(如果可用!(。有关详细信息,请参阅内容协商。

最新更新