Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON stream responses using application/json-seq generate 406 Not acceptable error #3347

Closed
dacr opened this issue Nov 28, 2023 · 2 comments
Closed

Comments

@dacr
Copy link

dacr commented Nov 28, 2023

Tapir version: 1.9.2

Scala version: 3.3.1

I get a 406 http Error: Not Acceptable when I try to change the response content type to application/json-seq

What is the problem?

I want to stream a sequence of json objects, the default content type application/json is not suitable as the output is streamed with no englobing json array syntax, we just have a stream of lines each line containing a valid json object

The endpoint is declared as follow :

    domainEndPoint
      .name("DomainListStream")
      .summary("List domains as stream")
      .post
      .in("listStream")
      .in(jsonBody[DomainFilter])
      .out(streamBody(ZioStreams)(Schema.derived[Domain], CodecFormat.Json()))
      .out(header(Header.contentType(MediaType.unsafeParse("application/json-seq"))))
      .out(statusCode(StatusCode.Ok).description("Domain list stream success"))
      .errorOutVariantPrepend(statusForBadFormatException)
      .errorOutVariantPrepend(statusForBackendException)
      .serverLogic[ThingInEnv](user => domainListStream(user))

I've found some interesting information, and it looks like the content type application/json-seq has been created for exactly this situation, unfortunately, it generates 406 error code in the current release of tapir, I have also noticed that MediaType doesn't contain a definition for this content type, no MediaType.ApplicationJsonSeq yet.

Or do we have an alternative in Tapir for this use case ? I didn't find anything yet in examples or using google.

@adamw
Copy link
Member

adamw commented Nov 28, 2023

I think it might be easiest to create a custom codec format:

  case class JsonSeq() extends CodecFormat {
    override val mediaType: MediaType = MediaType.unsafeApply("application", "json-seq")
  }

  streamBody(ZioStreams)(Schema.derived[Domain], JsonSeq())

are you still getting the error when the body is defined this way?

Otherwise, can you check what's the Accept header sent by the client?

As last resort, you can try disabling the NotAcceptableInterceptor using the server options

@dacr
Copy link
Author

dacr commented Nov 28, 2023

Thank you :) that was the issue, by using your suggestion it works fine now, still have an issue but this is related to the swagger user interface which is not yet able to understand json text sequences :

image

I close this ticket.

@dacr dacr closed this as completed Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants