armeria icon indicating copy to clipboard operation
armeria copied to clipboard

DocService to accept non JSON requests

Open ikhoon opened this issue 2 years ago • 1 comments

Discussed in https://github.com/line/armeria/discussions/5214

Originally posted by vthacker September 30, 2023 Hello Armeria folks,

I'm using annotated doc service @Post("/_bulk") that can also parse NDJSON strings. This is the only dataformat that Elasticsearch/Opensearch accepts ( https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html )

The service works as expected, however I am running into an issue with the DocService

I am passing this as my HTTP Headers within the doc service

{
  "content-type": "application/x-ndjson"
}

In the request body if I input NDJSON data, for example

{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }

The doc serice parser is complaining since it's expecting data to be JSON

Error: Failed to parse a JSON object in the request body:
SyntaxError: Unexpected non-whitespace character after JSON at position 49 (line 2 column 1)

So my first questionn is there a way for me to disable the JSON parsing of the request body?

My second quesion is the DocService dseems to overwrite the content-type header. For example if I change my request body to be a valid JSON

{ "index" : { "_index" : "test", "_id" : "1" } }

and then "Copy as a curl command" I see this

curl -XPOST -H 'content-type: application/json; charset=utf-8' https:/service.com'/_bulk' -d '{ "index" : { "_index" : "test", "_id" : "1" } }

If I change the HTTP Header to be

{
  "Content-Type": "application/x-ndjson"
}

then I see the request being formed as

curl -XPOST -H 'Content-Type: application/x-ndjson' -H 'content-type: application/json; charset=utf-8' https://service.com'/_bulk' -d '{ "index" : { "_index" : "test", "_id" : "1" } }

ikhoon avatar Oct 02 '23 13:10 ikhoon

I've created similar issues in the past. See https://github.com/line/armeria/issues/2198

heowc avatar Oct 05 '23 09:10 heowc