mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
BackendSrv: Fixes POST body for form data (#21714)
* BackendSrv: Fixes POST body for form data Fixes #21688 * Tests: Changes to POST examples
This commit is contained in:
parent
5166ffca33
commit
84ef0ebb17
@ -460,15 +460,32 @@ export class BackendSrv implements BackendService {
|
|||||||
return options.params && serializedParams.length ? `${options.url}?${serializedParams}` : options.url;
|
return options.params && serializedParams.length ? `${options.url}?${serializedParams}` : options.url;
|
||||||
};
|
};
|
||||||
|
|
||||||
private parseInitFromOptions = (options: BackendSrvRequest): RequestInit => ({
|
private parseInitFromOptions = (options: BackendSrvRequest): RequestInit => {
|
||||||
method: options.method,
|
const method = options.method;
|
||||||
headers: {
|
const headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Accept: 'application/json, text/plain, */*',
|
Accept: 'application/json, text/plain, */*',
|
||||||
...options.headers,
|
...options.headers,
|
||||||
},
|
};
|
||||||
body: typeof options.data === 'string' ? options.data : JSON.stringify(options.data),
|
const body = this.parseBody({ ...options, headers });
|
||||||
});
|
return {
|
||||||
|
method,
|
||||||
|
headers,
|
||||||
|
body,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
private parseBody = (options: BackendSrvRequest) => {
|
||||||
|
if (!options.data || typeof options.data === 'string') {
|
||||||
|
return options.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.headers['Content-Type'] === 'application/json') {
|
||||||
|
return JSON.stringify(options.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new URLSearchParams(options.data);
|
||||||
|
};
|
||||||
|
|
||||||
private getFromFetchStream = (options: BackendSrvRequest) => {
|
private getFromFetchStream = (options: BackendSrvRequest) => {
|
||||||
const url = this.parseUrlFromOptions(options);
|
const url = this.parseUrlFromOptions(options);
|
||||||
|
@ -170,13 +170,17 @@ describe('backendSrv', () => {
|
|||||||
|
|
||||||
describe('parseInitFromOptions', () => {
|
describe('parseInitFromOptions', () => {
|
||||||
it.each`
|
it.each`
|
||||||
method | headers | data | expected
|
method | headers | data | expected
|
||||||
${undefined} | ${undefined} | ${undefined} | ${{ method: undefined, headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/plain, */*' }, body: undefined }}
|
${undefined} | ${undefined} | ${undefined} | ${{ method: undefined, headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/plain, */*' }, body: undefined }}
|
||||||
${'GET'} | ${undefined} | ${undefined} | ${{ method: 'GET', headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/plain, */*' }, body: undefined }}
|
${'GET'} | ${undefined} | ${undefined} | ${{ method: 'GET', headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/plain, */*' }, body: undefined }}
|
||||||
${'GET'} | ${{ Auth: 'Some Auth' }} | ${undefined} | ${{ method: 'GET', headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/plain, */*', Auth: 'Some Auth' }, body: undefined }}
|
${'GET'} | ${{ Auth: 'Some Auth' }} | ${undefined} | ${{ method: 'GET', headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/plain, */*', Auth: 'Some Auth' }, body: undefined }}
|
||||||
${'GET'} | ${{ Auth: 'Some Auth' }} | ${{ data: { test: 'Some data' } }} | ${{ method: 'GET', headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/plain, */*', Auth: 'Some Auth' }, body: '{"data":{"test":"Some data"}}' }}
|
${'GET'} | ${{ Auth: 'Some Auth' }} | ${{ data: { test: 'Some data' } }} | ${{ method: 'GET', headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/plain, */*', Auth: 'Some Auth' }, body: '{"data":{"test":"Some data"}}' }}
|
||||||
${'GET'} | ${{ Auth: 'Some Auth' }} | ${'some data'} | ${{ method: 'GET', headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/plain, */*', Auth: 'Some Auth' }, body: 'some data' }}
|
${'GET'} | ${{ Auth: 'Some Auth' }} | ${'some data'} | ${{ method: 'GET', headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/plain, */*', Auth: 'Some Auth' }, body: 'some data' }}
|
||||||
${'GET'} | ${{ Auth: 'Some Auth' }} | ${'{"data":{"test":"Some data"}}'} | ${{ method: 'GET', headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/plain, */*', Auth: 'Some Auth' }, body: '{"data":{"test":"Some data"}}' }}
|
${'GET'} | ${{ Auth: 'Some Auth' }} | ${'{"data":{"test":"Some data"}}'} | ${{ method: 'GET', headers: { 'Content-Type': 'application/json', Accept: 'application/json, text/plain, */*', Auth: 'Some Auth' }, body: '{"data":{"test":"Some data"}}' }}
|
||||||
|
${'POST'} | ${{ Auth: 'Some Auth', 'Content-Type': 'application/x-www-form-urlencoded' }} | ${undefined} | ${{ method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', Accept: 'application/json, text/plain, */*', Auth: 'Some Auth' }, body: undefined }}
|
||||||
|
${'POST'} | ${{ Auth: 'Some Auth', 'Content-Type': 'application/x-www-form-urlencoded' }} | ${{ data: 'Some data' }} | ${{ method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', Accept: 'application/json, text/plain, */*', Auth: 'Some Auth' }, body: new URLSearchParams({ data: 'Some data' }) }}
|
||||||
|
${'POST'} | ${{ Auth: 'Some Auth', 'Content-Type': 'application/x-www-form-urlencoded' }} | ${'some data'} | ${{ method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', Accept: 'application/json, text/plain, */*', Auth: 'Some Auth' }, body: 'some data' }}
|
||||||
|
${'POST'} | ${{ Auth: 'Some Auth', 'Content-Type': 'application/x-www-form-urlencoded' }} | ${'{"data":{"test":"Some data"}}'} | ${{ method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', Accept: 'application/json, text/plain, */*', Auth: 'Some Auth' }, body: '{"data":{"test":"Some data"}}' }}
|
||||||
`(
|
`(
|
||||||
"when called with method: '$method', headers: '$headers' and data: '$data' then result should be '$expected'",
|
"when called with method: '$method', headers: '$headers' and data: '$data' then result should be '$expected'",
|
||||||
({ method, headers, data, expected }) => {
|
({ method, headers, data, expected }) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user