do not try to pass request body to get request

This commit is contained in:
kolaente 2025-07-19 16:50:34 +02:00
parent a747ed9ac2
commit 97c3e59821
No known key found for this signature in database
GPG Key ID: F40E70337AB24C9B
1 changed files with 26 additions and 16 deletions

View File

@ -102,17 +102,21 @@ class HttpClient {
const url = this.buildUrl(config)
const init: RequestInit = {method: config.method, headers: config.headers}
const body = this.prepareRequestBody(config.data, config.transformRequest)
// GET and HEAD requests cannot have a body
const method = config.method?.toUpperCase()
if (method !== 'GET' && method !== 'HEAD') {
const body = this.prepareRequestBody(config.data, config.transformRequest)
if (typeof body !== 'undefined') {
if (body instanceof FormData || body instanceof Blob) {
init.body = body as BodyInit
} else if (typeof body === 'string') {
init.body = body
} else {
init.body = JSON.stringify(body)
if (init.headers && !(init.headers as Record<string, string>)['Content-Type']) {
(init.headers as Record<string, string>)['Content-Type'] = 'application/json'
if (typeof body !== 'undefined') {
if (body instanceof FormData || body instanceof Blob) {
init.body = body as BodyInit
} else if (typeof body === 'string') {
init.body = body
} else {
init.body = JSON.stringify(body)
if (init.headers && !(init.headers as Record<string, string>)['Content-Type']) {
(init.headers as Record<string, string>)['Content-Type'] = 'application/json'
}
}
}
}
@ -170,13 +174,19 @@ class HttpClient {
reject(err)
}
const body = this.prepareRequestBody(config.data, config.transformRequest)
if (body instanceof FormData || body instanceof Blob) {
xhr.send(body as BodyInit)
} else if (typeof body === 'string' || typeof body === 'undefined') {
xhr.send(body)
// GET and HEAD requests cannot have a body
const method = config.method?.toUpperCase()
if (method === 'GET' || method === 'HEAD') {
xhr.send()
} else {
xhr.send(JSON.stringify(body))
const body = this.prepareRequestBody(config.data, config.transformRequest)
if (body instanceof FormData || body instanceof Blob) {
xhr.send(body as BodyInit)
} else if (typeof body === 'string' || typeof body === 'undefined') {
xhr.send(body)
} else {
xhr.send(JSON.stringify(body))
}
}
})
}