簡體   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