Skip to content

Commit

Permalink
fix(useFetch): doesn't work with formData payload (#2440)
Browse files Browse the repository at this point in the history
Content-Type should not be set for payload of formData type,  leave alone and let the browser do.

reference: [Automatic Content-Type for FormData uploads](axios/axios@2f4d0b8)
  • Loading branch information
abitwhy committed Dec 23, 2022
1 parent ed64fce commit 000fd62
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/core/useFetch/index.ts
Expand Up @@ -95,7 +95,6 @@ type Combination = 'overwrite' | 'chain'
const payloadMapping: Record<string, string> = {
json: 'application/json',
text: 'text/plain',
formData: 'multipart/form-data',
}

export interface BeforeFetchContext {
Expand Down Expand Up @@ -532,9 +531,9 @@ export function useFetch<T>(url: MaybeComputedRef<string>, ...args: any[]): UseF
}

const rawPayload = resolveUnref(config.payload)
// Set the payload to json type only if it's not provided and a literal object is provided
// Set the payload to json type only if it's not provided and a literal object is provided and the object is not `formData`
// The only case we can deduce the content type and `fetch` can't
if (!payloadType && rawPayload && Object.getPrototypeOf(rawPayload) === Object.prototype)
if (!payloadType && rawPayload && Object.getPrototypeOf(rawPayload) === Object.prototype && !(rawPayload instanceof FormData))
config.payloadType = 'json'

return {
Expand Down

0 comments on commit 000fd62

Please sign in to comment.