Skip to content

feat(client): allow overriding of Accept header per request #347

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

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/runtime/interceptors/common/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@ export default async function handleRequestHeaders(
logger: ConsolaInstance,
): Promise<void> {
const method = ctx.options.method?.toLowerCase() ?? 'get'
const headersToAdd = { Accept: 'application/json' }

ctx.options.headers = appendRequestHeaders(ctx.options.headers, headersToAdd)
if (!ctx.options.headers?.has('Accept')) {
const headersToAdd = { Accept: 'application/json' }

ctx.options.headers = appendRequestHeaders(
ctx.options.headers,
headersToAdd,
)

logger.debug(
'[request] added default headers',
Object.keys(headersToAdd),
)
}

// https://laravel.com/docs/10.x/routing#form-method-spoofing
if (method === 'put' && ctx.options.body instanceof FormData) {
ctx.options.method = 'POST'
ctx.options.body.append('_method', 'PUT')
}

logger.debug(
'[request] added default headers',
Object.keys(headersToAdd),
)
}