繁体   English   中英

Fetch API返回415(不支持的媒体类型)

[英]Fetch API is returning 415 (Unsupported Media Type)

我想用fetch api(或unfetch)替换axios包,但由于某种原因,fetch对我不起作用。 Axios工作正常,所以我用fetch替换它,但是返回415 (Unsupported Media Type) 我使用它通过POST请求将文件上传到服务器。 也许我在fetch设置中遗漏了一些东西?

FORMDATA

const upload = file.length > 0 && file[0]
formData.append('file', upload, upload.name)

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()}`,
  },
})

抓取(不工作)


const { data: { small, large } } = await fetch(`${API_URL}/upload/avatar`, {
  method: 'POST',
  body: formData,
  headers: {
    'Content-Type': 'multipart/form-data',
    Authorization: `Bearer ${getTokenFromCookie()}`,
  },
})

我想你错过了一个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM