Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useFetch): doesn't work with formData payload #2440

Merged
merged 2 commits into from Dec 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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