如何在 Axios 的 headers 中设置 `Content-Type`?
How to set `Content-Type` in headers in Axios?
我在 axios 中设置 Content-Type
header 时遇到问题。
这是我的代码:
axios({
url: fetchUrl,
data: JSON.stringify(fetchOptions.body),
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
Accept: 'application/vnd.api+json',
},
})
这是请求 headers:
Accept
设置正确,但 Content-Type
设置不正确。 (通过在我的代码中删除 Accept
进行确认,在这种情况下请求 header 恢复为 json
而不是 vnd.api+json
。)
当我将 Content-Type
更改为 ContentType
时,我会在响应 header 中看到 ContentType
,因此问题出在 Content-Type
上。
原来这个错误是空的data
; 属性 被称为 data
,但我错误地称为 body
:
axios({
url: fetchUrl,
data: JSON.stringify(fetchOptions.data),
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
Accept: 'application/vnd.api+json',
},
})
我在 axios 中设置 Content-Type
header 时遇到问题。
这是我的代码:
axios({
url: fetchUrl,
data: JSON.stringify(fetchOptions.body),
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
Accept: 'application/vnd.api+json',
},
})
这是请求 headers:
Accept
设置正确,但 Content-Type
设置不正确。 (通过在我的代码中删除 Accept
进行确认,在这种情况下请求 header 恢复为 json
而不是 vnd.api+json
。)
当我将 Content-Type
更改为 ContentType
时,我会在响应 header 中看到 ContentType
,因此问题出在 Content-Type
上。
原来这个错误是空的data
; 属性 被称为 data
,但我错误地称为 body
:
axios({
url: fetchUrl,
data: JSON.stringify(fetchOptions.data),
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
Accept: 'application/vnd.api+json',
},
})