From d9d93fa14a4e03efe760f34d9161c7e62e30c0a6 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 14 Nov 2024 21:17:37 +1100 Subject: [PATCH] [8.x] [Console] Fix incorrect output message when status code is 200 and body is empty (#199975) (#200122) # Backport This will backport the following commits from `main` to `8.x`: - [[Console] Fix incorrect output message when status code is 200 and body is empty (#199975)](https://github.com/elastic/kibana/pull/199975) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Ignacio Rivas --- .../use_send_current_request/send_request.ts | 14 +++++++++++--- test/functional/apps/console/_console.ts | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts index 101831300cb89..e8c1715b96c61 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts +++ b/src/plugins/console/public/application/hooks/use_send_current_request/send_request.ts @@ -157,10 +157,18 @@ export function sendRequest(args: RequestArgs): Promise { const { statusCode, statusText } = extractStatusCodeAndText(response, path); - if (body) { - value = JSON.stringify(body, null, 2); + // When the request is sent, the HTTP library tries to parse the response body as JSON. + // However, if the response body is empty or not in valid JSON format, it throws an error. + // To handle this, if the request resolves with a 200 status code but has an empty or invalid body, + // we should still display a success message to the user. + if (statusCode === 200 && body === null) { + value = 'OK'; } else { - value = 'Request failed to get to the server (status code: ' + statusCode + ')'; + if (body) { + value = JSON.stringify(body, null, 2); + } else { + value = 'Request failed to get to the server (status code: ' + statusCode + ')'; + } } if (isMultiRequest) { diff --git a/test/functional/apps/console/_console.ts b/test/functional/apps/console/_console.ts index 88f87bb30e2b9..4389e4f275e01 100644 --- a/test/functional/apps/console/_console.ts +++ b/test/functional/apps/console/_console.ts @@ -215,5 +215,21 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { expect(await PageObjects.console.hasSuccessBadge()).to.be(true); }); }); + + it('Shows OK when status code is 200 but body is empty', async () => { + await PageObjects.console.clearEditorText(); + + // This request will return 200 but with an empty body + await PageObjects.console.enterText( + 'POST /_cluster/voting_config_exclusions?node_names=node' + ); + await PageObjects.console.clickPlay(); + + await retry.try(async () => { + const actualResponse = await PageObjects.console.getOutputText(); + log.debug(actualResponse); + expect(actualResponse).to.contain('OK'); + }); + }); }); }