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

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

curl -XPOST '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.

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.

@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.

@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.

@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?

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

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