From 6203f9b6a44960750da1a7bbb26f9435f2d80ea6 Mon Sep 17 00:00:00 2001 From: Elena Stoeva <59341489+ElenaStoeva@users.noreply.github.com> Date: Tue, 23 Jul 2024 18:06:20 +0100 Subject: [PATCH] [Console] Fix failing bulk requests (#188552) Fixes https://github.com/elastic/kibana/issues/186633 ## Summary This PR fixes the failing Bulk requests in Console Monaco and in Console Ace when they are indented. The reason for the failures is that, when the request body contains multiple data objects, ES accepts it only if each data object is in a single line. **How to test:** Test in both Console with Monaco and old Console (by using the `console.dev.enableMonaco: false` setting in `config/kibana.dev.yml`) by sending bulk requests as well as regular requests. Try with formatted and unformatted requests. Also try sending multiple requests at once. https://github.com/user-attachments/assets/42515d69-cc8c-4874-a18a-f5fe0ee604ca Some bulk requests to test with: ``` PUT /library/_bulk?refresh {"index":{"_id":"Leviathan Wakes"}} {"name":"Leviathan Wakes","author":"James S.A. Corey","release_date":"2011-06-02","page_count":561} {"index":{"_id":"Hyperion"}} {"name":"Hyperion","author":"Dan Simmons","release_date":"1989-05-26","page_count":482} {"index":{"_id":"Dune"}} {"name":"Dune","author":"Frank Herbert","release_date":"1965-06-01","page_count":604} ``` ``` POST _bulk { "index" : { "_index" : "test", "_id" : "1" } } { "field1" : "value1" } { "delete" : { "_index" : "test", "_id" : "2" } } { "create" : { "_index" : "test", "_id" : "3" } } { "field1" : "value3" } { "update" : {"_id" : "1", "_index" : "test"} } { "doc" : {"field2" : "value2"} } ``` --- .../hooks/use_send_current_request/send_request.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 9dee09c5ef3bf..5b047b43fcf48 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 @@ -83,7 +83,13 @@ export function sendRequest(args: RequestArgs): Promise { const req = requests.shift()!; const path = req.url; const method = req.method; - let data = collapseLiteralStrings(req.data.join('\n')); + + // If the request data contains multiple data objects (e.g. bulk request) + // ES only accepts it if each object is on a single line + // Therefore, we need to remove all new line characters from each data object + const unformattedData = req.data.map((body) => body.replaceAll('\n', '')); + + let data = collapseLiteralStrings(unformattedData.join('\n')); if (data) { data += '\n'; } // append a new line for bulk requests.