[英]Fetch API is returning 415 (Unsupported Media Type)
I would like to replace the axios package with the fetch api (or unfetch) but for some reason, fetch doesn't work for me. 我想用fetch api(或unfetch)替换axios包,但由于某种原因,fetch对我不起作用。 Axios is working just fine, so I replaced it with fetch but that is returning
415 (Unsupported Media Type)
. Axios工作正常,所以我用fetch替换它,但是返回
415 (Unsupported Media Type)
。 I use it to upload a file to the server with a POST request. 我使用它通过POST请求将文件上传到服务器。 Maybe I am missing something in the fetch setup?
也许我在fetch设置中遗漏了一些东西?
FormData FORMDATA
const upload = file.length > 0 && file[0]
formData.append('file', upload, upload.name)
Axios (working) Axios(工作)
const {
data: { small, large },
} = await axios({
method: 'POST',
url: `${API_URL}/upload/avatar`,
data: formData,
headers: {
'Content-Type': 'multipart/form-data',
Authorization: `Bearer ${getTokenFromCookie()}`,
},
})
Fetch (not working) 抓取(不工作)
const { data: { small, large } } = await fetch(`${API_URL}/upload/avatar`, {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'multipart/form-data',
Authorization: `Bearer ${getTokenFromCookie()}`,
},
})
I think you're missing an Accept
header: 我想你错过了一个
Accept
标题:
const { data: { small, large } } = await fetch(`${API_URL}/upload/avatar`, {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'multipart/form-data',
'Authorization': `Bearer ${getTokenFromCookie()}`,
'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
},
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.