zio / zio-http

A next-generation Scala framework for building scalable, correct, and efficient HTTP clients and servers
https://zio.dev/zio-http
Apache License 2.0
787 stars 396 forks source link

Endpoint API:‌ Support `octet-stream` Media-type For Multipart Form Fields #2746

Closed khajavi closed 3 months ago

khajavi commented 6 months ago

I expect to have binary form fields to upload files in the Endpoint API using the octet-stream media type, but currently, the Endpoint API doesn't support such media type and throws an exception.


import zio._
import zio.http._
import zio.http.endpoint.Endpoint

object MultipartFormDataEndpointExample extends ZIOAppDefault {
  def run =
    Server
      .serve(
        Endpoint(RoutePattern.POST / "books")
          .in[String]("title")
          .in[String]("author")
          .in[Chunk[Byte]](MediaType.application.`octet-stream`, "file")
          .out[Unit]
          .implement(handler { (title: String, author: String, file: Chunk[Byte]) =>
            ZIO.debug("Handler called!")
          })
          .toHttpApp @@ Middleware.debug,
      )
      .provide(Server.default, Scope.default)
}

The console output when I run the above example:

import zio._
import zio.http._
import zio.http.endpoint.Endpoint

object MultipartFormDataEndpointExample extends ZIOAppDefault {
  def run =
    Server
      .serve(
        Endpoint(RoutePattern.POST / "books")
          .in[String]("title")
          .in[String]("author")
          .in[Chunk[Byte]](MediaType.application.`octet-stream`, "file")
          .out[Unit]
          .implement(handler { (title: String, author: String, file: Chunk[Byte]) =>
            ZIO.debug("Handler called!")
          })
          .toHttpApp @@ Middleware.debug,
      )
      .provide(Server.default, Scope.default)
}
jdegoes commented 4 months ago

/bounty $150

algora-pbc[bot] commented 4 months ago

💎 $150 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #2746 with your implementation plan
  2. Submit work: Create a pull request including /claim #2746 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 #2926
algora-pbc[bot] commented 3 months ago

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

algora-pbc[bot] commented 3 months ago

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