# The msearch request must be terminated by a newline

**URL:** <https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline/142586>\
**Category:** Elasticsearch\
**Created:** [August 1, 2018, 2:08pm UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline/142586 "2018-08-01T14:08:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zwokaros](https://avatars.discourse-cdn.com/v4/letter/z/f05b48/32.png) [@zwokaros](https://discuss.elastic.co/u/zwokaros)\
**Post date:** [August 1, 2018, 2:08pm UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline/142586/1 "2018-08-01T14:08:28Z")

</div>

Hello

I've been stuck on this problem

Basically I have a curl POST request that works when it's written directly on a terminal, but whenever I try to do it in a script, I get this error

`$data_string = '{"index":["tracker"],"ignore_unavailable":true,"preference":1533127804501}{"size":0,"_source":{"excludes":[]},"aggs":{"2":{"terms":{"field":"brand.name.keyword","size":500,"order":{"_count":"desc"}},"aggs":{"3":{"terms":{"field":"trackerType.keyword","size":50,"order":{"_count":"desc"}}}}}},"stored_fields":["*"],"script_fields":{},"docvalue_fields":["date"],"query":{"bool":{"must":[{"match_all":{}},{"range":{"date":{"gte":1533041405943,"lte":1533127805943,"format":"epoch_millis"}}}],"filter":[],"should":[],"must_not":[]}}}';`

I've tried putting newlines everywhere (first at the logic places, then I slowly became paranoid about everything and re-evaluated my existence)

But I still end up with this error

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

Please send help

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 2, 2018, 9:20am UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline/142586/2 "2018-08-02T09:20:55Z")

</div>

Hey,

it is impossible to take a look at the request you actually sent to elasticsearch, so this is super hard to debug. The error message says, that the last character of your msearch body should be a new line. Is this the case?

--Alex

---

<div class="post-metadata">

**Author:** ![zwokaros](https://avatars.discourse-cdn.com/v4/letter/z/f05b48/32.png) [@zwokaros](https://discuss.elastic.co/u/zwokaros)\
**Post date:** [August 2, 2018, 11:51am UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline/142586/3 "2018-08-02T11:51:49Z")

</div>

Hello!

I actually had it working this morning, all I did (even tho I'm pretty sure I've tried that yesterday) is add two \n in the json request

```
$data_string = '{"index":["tracker"],"ignore_unavailable":true,"preference":1533127804501}'."\n".'{"size":0,"_source":{"excludes":[]},"aggs":{"2":{"terms":{"field":"brand.name.keyword","size":500,"order":{"_count":"desc"}},"aggs":{"3":{"terms":{"field":"trackerType.keyword","size":50,"order":{"_count":"desc"}}}}}},"stored_fields":["*"],"script_fields":{},"docvalue_fields":["date"],"query":{"bool":{"must":[{"match_all":{}},{"range":{"date":{"gte":'.$newRapport.',"lte":'.($newRapport += 86400000).',"format":"epoch_millis"}}}],"filter":[],"should":[],"must_not":[]}}}'."\n";

```

Elastic search sure has a strange formatting

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 2, 2018, 12:49pm UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline/142586/4 "2018-08-02T12:49:39Z")

</div>

> [@zwokaros](#):
>
> Elastic search sure has a strange formatting

Same reasons the Post Office expect you to put letters in envelopes - they're quicker to sort and distribute if the distributor only deals with the routing information and they don't have to parse the details of the letter contents.

---

<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:** [August 30, 2018, 12:49pm UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline/142586/5 "2018-08-30T12:49:41Z")

</div>

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