# The msearch request must be terminated by a newline \[\\n\] error

**URL:** <https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline-n-error/211846>\
**Category:** Elasticsearch\
**Created:** [December 13, 2019, 9:01pm UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline-n-error/211846 "2019-12-13T21:01:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aasoft](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@aasoft](https://discuss.elastic.co/u/aasoft)\
**Post date:** [December 13, 2019, 9:01pm UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline-n-error/211846/1 "2019-12-13T21:01:15Z")

</div>

I am trying to use the curl command and hit my index. I am using the following command

curl -XPOST '[https://myurl/myindex/\_msearch?'](https://myurl/myindex/_msearch?') -H 'Content-Type: application/x-ndjson' -d'{"preference":"ResultList01"}  
{"query": {"match\_all": {}}}  
'  
I am getting the following 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}

The error says I need to use the new line and I am doing it as you see in above.

The query is working fine with Kibana.

---

<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:** [December 16, 2019, 9:00am UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline-n-error/211846/2 "2019-12-16T09:00:03Z")

</div>

copy pasting your call worked fine for me. What operating system is this on, and what shell are you using? Maybe there is some magic going on. What you could try instead, is to write the body of the request to a file, make sure there is a line ending at the end and then call curl with the `-d @my-file.mjson` command, which will read the body from that file.

---

<div class="post-metadata">

**Author:** ![aasoft](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@aasoft](https://discuss.elastic.co/u/aasoft)\
**Post date:** [December 17, 2019, 6:37pm UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline-n-error/211846/3 "2019-12-17T18:37:08Z")

</div>

@spinscale Thanks for looking into this. I saved the file at /home/myname/myquery.json  
When I run  
-d @/home/myname/myquery.json  
I am getting this error:  
Couldn't read data from file "/home/myname/myquery.json", this makes an empty POST.  
Never executed the curl from a file.  
Some more info;  
The request is running in Unix and the query is created in proxy. Within the proxy (node js) I use any of the following to embed new line at the end of the query (body) like:  
body = eol.after(body);  
or  
body = body + os.EOL;  
or  
body = body + "\n";  
in any case when I call:  
proxyReq.write(body);  
The return value is:  
"The msearch request must be terminated by a newline."  
Even if I hard code the body with a valid query ending by "\n", I get the same message back.

---

<div class="post-metadata">

**Author:** ![aasoft](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@aasoft](https://discuss.elastic.co/u/aasoft)\
**Post date:** [December 22, 2019, 9:07pm UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline-n-error/211846/4 "2019-12-22T21:07:38Z")

</div>

@spinscale, I narrow down my problem. I need to modify the query in proxy before sending it to elasticsearch.  
When I pass the following body it is working:  
body = bod[0] + '\n{"query": {"match\_all": {}}}\n'  
When I use the following  
body = bod[0] + '\n{"query": {"term": {"control\_num": 1}}}\n'  
I am getting the following error:  
The msearch request must be terminated by a newline  
This definitely has nothing to do with '\n' for end of line because both codes have this value.  
I am using http-proxy-middleware and I using proxyReq.write(body); to send the body.  
any help is appreciated.

---

<div class="post-metadata">

**Author:** ![aasoft](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@aasoft](https://discuss.elastic.co/u/aasoft)\
**Post date:** [December 23, 2019, 1:26pm UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline-n-error/211846/5 "2019-12-23T13:26:57Z")

</div>

@spinscale  
Looks like the query you make should follow specific format. For example ":" should come right after "query" without having a space in between. For after ":" it should have one space and not more and "so forth".  
I don't know about the "so forth". What other rules should be in place in order to submit the query correct? For example if you have comma in your query, do you have a leading space before or after it?  
In Kiana such a restrict rules doesn't exist.  
Is there any sample workable JSON file available that is running successfully with Curl so I can follow the rule?

---

<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:** [January 8, 2020, 10:10am UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline-n-error/211846/6 "2020-01-08T10:10:56Z")

</div>

there are no rules about the spaces, the problem will likely be somewhere else. The important part are the newlines and beginning with `{` IIRC.

---

<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:** [February 5, 2020, 10:11am UTC](https://discuss.elastic.co/t/the-msearch-request-must-be-terminated-by-a-newline-n-error/211846/7 "2020-02-05T10:11:01Z")

</div>

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