0

I am using AWS lambda as a proxy for my ElasticSearch service. I am facing issues with proxying requests to Multi search API. As this end point accepts payload in the form of ndjson, I am trying to send the following payload as request body and Content-Type as application/x-ndjson. I tried different content-types but no use.

{"preference":"SearchResult"}\n
{"query":{"match_all":{}},"size":5,"from":0}

This request fails with a JSON syntax error.

{
    "errorType": "SyntaxError",
    "errorMessage": "Unexpected token \\ in JSON at position 29",
    "trace": [
       "SyntaxError: Unexpected token \\ in JSON at position 29",
       "    at JSON.parse (<anonymous>)",
       "    at Runtime.handleOnce (/var/runtime/Runtime.js:67:14)"
    ]
}

This error is expected as lambda is trying to parse the request as JSON. So obviously the call is not reaching the Lambda handler function. How can I make Lambda function to work with ndjson? Is it possible?

user3731783
  • 718
  • 1
  • 7
  • 31
user1868744
  • 963
  • 1
  • 13
  • 27
  • There's no `ndjson`. That's just a marketing attempt that tried to hijack a common way of streaming data, by sending each JSON object in a separate line. There's no such content type. – Panagiotis Kanavos Nov 27 '19 at 15:47
  • As for the error, I suspect you added `\ ` and `n` in your JSON string. Elastic expects to find one JSON object *per line* though. It expects an actual newline character after each object, not an escape sequence – Panagiotis Kanavos Nov 27 '19 at 15:49
  • BTW the "spec" in that site used to be a copy from `json.org`. Looks like the author received a phone call from an ECMA lawyer. The "spec" right now is just a readme in a Github repo – Panagiotis Kanavos Nov 27 '19 at 15:54

0 Answers0