Commit a0c6afa0 by Hugo Häggmark Committed by Arve Knudsen

Fix: fixes issue with headers property with different casing (#22778)

Fixes #22756

(cherry picked from commit b30f4c7b)
parent 3d0bc141
...@@ -589,12 +589,10 @@ export const parseUrlFromOptions = (options: BackendSrvRequest): string => { ...@@ -589,12 +589,10 @@ export const parseUrlFromOptions = (options: BackendSrvRequest): string => {
export const parseInitFromOptions = (options: BackendSrvRequest): RequestInit => { export const parseInitFromOptions = (options: BackendSrvRequest): RequestInit => {
const method = options.method; const method = options.method;
const headers = { const headers = parseHeaders(options);
'Content-Type': 'application/json', const isAppJson = isContentTypeApplicationJson(headers);
Accept: 'application/json, text/plain, */*', const body = parseBody(options, isAppJson);
...options.headers,
};
const body = parseBody({ ...options, headers });
return { return {
method, method,
headers, headers,
...@@ -602,14 +600,42 @@ export const parseInitFromOptions = (options: BackendSrvRequest): RequestInit => ...@@ -602,14 +600,42 @@ export const parseInitFromOptions = (options: BackendSrvRequest): RequestInit =>
}; };
}; };
const parseBody = (options: BackendSrvRequest) => { export const parseHeaders = (options: BackendSrvRequest) => {
if (!options.data || typeof options.data === 'string') { const headers = new Headers({
return options.data; 'Content-Type': 'application/json',
Accept: 'application/json, text/plain, */*',
});
if (options && options.headers) {
Object.keys(options.headers).forEach(key => {
headers.set(key, options.headers[key]);
});
}
return headers;
};
export const isContentTypeApplicationJson = (headers: Headers) => {
if (!headers) {
return false;
}
const contentType = headers.get('content-type');
if (contentType && contentType.toLowerCase() === 'application/json') {
return true;
}
return false;
};
export const parseBody = (options: BackendSrvRequest, isAppJson: boolean) => {
if (!options) {
return options;
} }
if (options.headers['Content-Type'] === 'application/json') { if (!options.data || typeof options.data === 'string') {
return JSON.stringify(options.data); return options.data;
} }
return new URLSearchParams(options.data); return isAppJson ? JSON.stringify(options.data) : new URLSearchParams(options.data);
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment