Elasticsearch bulk error - must be terminated by a newline

trying to use a elasticsearch bulk api. e.g. the following bulk request succeeds

$ curl -X POST "localhost:9200/test/_bulk?pretty" -H 'Content-Type: application/json' -d '
{"index":{"_id":1}}
{"foo":"foo"}
{"index":{"_id":2}}
{"baz":"baz"}
'
{
  "took" : 569,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "test",
        "_id" : "1",
        "_version" : 5,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 4852,
        "_primary_term" : 1,
        "status" : 200
      }
    },
    {
      "index" : {
        "_index" : "test",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 4853,
        "_primary_term" : 1,
        "status" : 201
      }
    }
  ]
}

switching from curl to nodejs, an error is thrown. for instance, given the file bulk.js

// bulk.js
import axios from "axios";

const run = async () => {
  const buff = [
    { index: { "_id": 1 } },
    { foo: 'foo' },
    { index: { "_id": 2 } },
    { baz: 'baz' }
  ]
    .map(doc => JSON.stringify(doc))
    .join("\n")
    + "\n";
  console.log(buff);
  const client = axios.create({
    baseURL: "http://localhost:9200",
    headers: { "Content-Type": "application/json" },
  });
  try {
    const response = await client.post("test/_bulk", buff);
    console.log(response.data);
  } catch(err) {
    console.log(JSON.stringify(err.response.data, null, 2));
  }
}

run();

when it runs, error returns

$ node bulk.js
{"index":{"_id":1}}
{"foo":"foo"}
{"index":{"_id":2}}
{"baz":"baz"}

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "The bulk request must be terminated by a newline [\\n]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "The bulk request must be terminated by a newline [\\n]"
  },
  "status": 400
}

what causes the issue?

Please change your header to below and it should work.

headers: { "Content-Type": "application/x-ndjson" }

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.