How to send multiple JSON root elements in one request

I am trying to send bulk request from k6 to elasticsearch and return error “SyntaxError: Unexpected token at the end: { at parse (native)”

This is due to multiple JSON root elements as below:
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”} }
Referring: Bulk API | Elasticsearch Guide [8.4] | Elastic

This works fine in curl commands but not in k6.
Is there a ways to achieve this in K6?

Hi @rolandyh !

Welcome to the community forums! :wave:

How do you render a request body?

For example, does something like:

import { sleep } from 'k6';
import http from 'k6/http';

export default function () {
  
   let 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"} }
   ];


   let body = "";
   bulk.forEach((j) => {
      body += JSON.stringify(j) + "\n";
   });

   http.post("http://localhost:8085/", body);

  sleep(1);
}

work for you?

Let me know if that helps,
Cheers!