From 000fd62b05f64add5fddcfec88570a3272d8c160 Mon Sep 17 00:00:00 2001 From: abitwhy <68452982+abitwhy@users.noreply.github.com> Date: Sat, 24 Dec 2022 03:39:10 +0800 Subject: [PATCH] fix(useFetch): doesn't work with formData payload (#2440) 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](https://github.com/axios/axios/commit/2f4d0b8b45dbb766a3348200fdff02ee00d9d6c0) --- packages/core/useFetch/index.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/core/useFetch/index.ts b/packages/core/useFetch/index.ts index c5d864c920c..c03808e35e5 100644 --- a/packages/core/useFetch/index.ts +++ b/packages/core/useFetch/index.ts @@ -95,7 +95,6 @@ type Combination = 'overwrite' | 'chain' const payloadMapping: Record = { json: 'application/json', text: 'text/plain', - formData: 'multipart/form-data', } export interface BeforeFetchContext { @@ -532,9 +531,9 @@ export function useFetch(url: MaybeComputedRef, ...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 {