Request payload is not valid JSON


I’m attempting to proxy to Grafana via an express app that I have created. The body parser middleware that is used, however, doesn’t like the request payloads that are used to query my elasticsearch datasource.

An example request payload:


An example of the error that I receive is:

SyntaxError: Unexpected token { in JSON at position 82
    at JSON.parse (<anonymous>)
    at parse (/srv/www/node_modules/body-parser/lib/types/json.js:89:19)
    at /srv/www/node_modules/body-parser/lib/read.js:121:18
    at invokeCallback (/srv/www/node_modules/raw-body/index.js:224:16)
    at done (/srv/www/node_modules/raw-body/index.js:213:7)
    at IncomingMessage.onEnd (/srv/www/node_modules/raw-body/index.js:273:7)
    at IncomingMessage.emit (events.js:197:13)
    at endReadableNT (_stream_readable.js:1129:12)
    at processTicksAndRejections (internal/process/next_tick.js:76:17)

Any thoughts? Is this just a quirk of the Elasticsearch API?

I think I figured it out. It really is just an elasticsearch quirk. Instead of parsing all POST requests that came through I am now selectively parsing POST request payloads and that resolved my issue.