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

Endpoint API: Endpoint#outCodec Doesn't Respect The Specified HeaderCodec #2748

Closed
khajavi opened this issue Mar 27, 2024 · 4 comments · Fixed by #2896
Closed

Endpoint API: Endpoint#outCodec Doesn't Respect The Specified HeaderCodec #2748

khajavi opened this issue Mar 27, 2024 · 4 comments · Fixed by #2896
Labels
💎 Bounty bug Something isn't working 💰 Rewarded

Comments

@khajavi
Copy link
Member

khajavi commented Mar 27, 2024

Describe the bug
In the following example I expect the Endpoint API, to respect the header codec HeaderCodec.contentType.expect(Header.ContentType(MediaType.multipart.mixed/)) so the content type of the response should be multipart/mixed but the current content type returned by this route is multipart/form-data

To Reproduce
Steps to reproduce the behavior:

run the following example:

import zio._
import zio.http._
import zio.http.codec._
import zio.http.endpoint.Endpoint
import zio.http.endpoint.EndpointMiddleware.None
import zio.schema.DeriveSchema.gen
import zio.schema._
import zio.stream.ZStream

case class Book(title: String, authors: List[String])
object Book {
  implicit val schema = DeriveSchema.gen[Book]
}

object EndpointExample extends ZIOAppDefault {
  val endpoint: Endpoint[Int, Int, ZNothing, (Book, ZStream[Any, Nothing, Byte]), None] =
    Endpoint(RoutePattern.GET / "books" / PathCodec.int("id"))
      .outCodec(
        HttpCodec.content[Book]("book", MediaType.application.`json`) ++
          HttpCodec.binaryStream("file", MediaType.application.`octet-stream`) ++
          HeaderCodec.contentType.expect(Header.ContentType(MediaType.multipart.`mixed`)),
      )

  def run = Server
    .serve(
      endpoint
        .implement(handler { (id: Int) =>
          (Book("John's Book", List("John Doe")), ZStream.from(Chunk.fromArray("the book file".getBytes)))
        })
        .toHttpApp @@ Middleware.debug,
    )
    .provide(Server.default, Scope.default)
}

Then run the following curl command:

curl -X GET 'http://127.0.0.1:8080/books/123' -i

Expected behavior

HTTP/1.1 200 OK
content-type: multipart/mixed
transfer-encoding: chunked

------zio-http-boundary-D4792A5C-93E0-43B5-9A1F-48E38FDE5714
Content-Disposition: form-data; name="book"
Content-Type: application/json

{"title":"John's Book","authors":["John Doe"]}
------zio-http-boundary-D4792A5C-93E0-43B5-9A1F-48E38FDE5714
Content-Disposition: form-data; name="file"
Content-Type: application/octet-stream

the book file
------zio-http-boundary-D4792A5C-93E0-43B5-9A1F-48E38FDE5714--
@khajavi khajavi added the bug Something isn't working label Mar 27, 2024
@jdegoes
Copy link
Member

jdegoes commented Jun 5, 2024

/bounty $150

Copy link

algora-pbc bot commented Jun 5, 2024

💎 $150 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #2748 with your implementation plan
  2. Submit work: Create a pull request including /claim #2748 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio-http!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🟢 @987Nabil #2896

Copy link

algora-pbc bot commented Jun 7, 2024

💡 @987Nabil submitted a pull request that claims the bounty. You can visit your bounty board to reward.

Copy link

algora-pbc bot commented Jun 8, 2024

🎉🎈 @987Nabil has been awarded $150! 🎈🎊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💎 Bounty bug Something isn't working 💰 Rewarded
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants