diff --git a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts index f46acbf6cb71..0a401ded813b 100644 --- a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts +++ b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts @@ -70,13 +70,15 @@ function getProxyHeaders(req: OpenSearchDashboardsRequest) { } function toUrlPath(path: string) { - let urlPath = `/${trimStart(path, '/')}`; + const FAKE_BASE = 'http://localhost'; + const urlWithFakeBase = new URL(`${FAKE_BASE}/${trimStart(path, '/')}`); // Appending pretty here to have OpenSearch do the JSON formatting, as doing // in JS can lead to data loss (7.0 will get munged into 7, thus losing indication of // measurement precision) - if (!urlPath.includes('?pretty')) { - urlPath += '?pretty=true'; + if (!urlWithFakeBase.searchParams.get('pretty')) { + urlWithFakeBase.searchParams.append('pretty', 'true'); } + const urlPath = urlWithFakeBase.href.replace(urlWithFakeBase.origin, ''); return urlPath; } diff --git a/src/plugins/console/server/routes/api/console/proxy/tests/query_string.test.ts b/src/plugins/console/server/routes/api/console/proxy/tests/query_string.test.ts index 105bce3bfad2..9aefea1182cc 100644 --- a/src/plugins/console/server/routes/api/console/proxy/tests/query_string.test.ts +++ b/src/plugins/console/server/routes/api/console/proxy/tests/query_string.test.ts @@ -82,6 +82,14 @@ describe('Console Proxy Route', () => { expect(args.path).toBe('/index/id?pretty=true'); }); }); + + describe(`contains query parameter`, () => { + it('adds slash to path before sending request', async () => { + await request('GET', '_cat/tasks?v'); + const [[args]] = opensearchClient.asCurrentUser.transport.request.mock.calls; + expect(args.path).toBe('/_cat/tasks?v=&pretty=true'); + }); + }); }); }); });