# Elasticsearch bulk error - must be terminated by a newline

**URL:** <https://discuss.elastic.co/t/elasticsearch-bulk-error-must-be-terminated-by-a-newline/299037>\
**Category:** Elasticsearch\
**Created:** [March 8, 2022, 4:56am UTC](https://discuss.elastic.co/t/elasticsearch-bulk-error-must-be-terminated-by-a-newline/299037 "2022-03-08T04:56:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kobalt](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@kobalt](https://discuss.elastic.co/u/kobalt)\
**Post date:** [March 8, 2022, 4:56am UTC](https://discuss.elastic.co/t/elasticsearch-bulk-error-must-be-terminated-by-a-newline/299037/1 "2022-03-08T04:56:26Z")

</div>

trying to use a [elasticsearch bulk api](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html). e.g. the following bulk request succeeds

```nohighlight
$ 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`

```javascript
// 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

```nohighlight
$ 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?

---

<div class="post-metadata">

**Author:** ![sagarpatel](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@sagarpatel](https://discuss.elastic.co/u/sagarpatel)\
**Post date:** [March 8, 2022, 5:20am UTC](https://discuss.elastic.co/t/elasticsearch-bulk-error-must-be-terminated-by-a-newline/299037/2 "2022-03-08T05:20:41Z")

</div>

Please change your header to below and it should work.

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 5, 2022, 5:20am UTC](https://discuss.elastic.co/t/elasticsearch-bulk-error-must-be-terminated-by-a-newline/299037/3 "2022-04-05T05:20:58Z")

</div>

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