zio-http icon indicating copy to clipboard operation
zio-http copied to clipboard

Added support for specifying multiple media types for a single codec

Open abdelfetah18 opened this issue 5 months ago • 1 comments

/claim #3284

// Before – this doesn't work correctly:
val mediaTypeJsonOrTextEndpoint = Endpoint(RoutePattern.POST / "api" / "mediaTypeJsonOrText")
  .outCodec(HttpCodec.content[Greeting](MediaType.text.plain))
  .outCodec(HttpCodec.content[Greeting](MediaType.application.json))

This approach fails because both codecs are defined for the same type (Greeting). The fallback mechanism isn't smart enough to distinguish based on media type and will always select the first codec, even if the client expects a different content type.

My Solution Is:

// Added support for multiple media types:
val mediaTypeJsonOrTextEndpoint = Endpoint(RoutePattern.POST / "api" / "mediaTypeJsonOrText")
  .outCodec(HttpCodec.content[Greeting](List(MediaType.text.plain, MediaType.application.json)))

To resolve this, I added support for specifying multiple media types for a single codec. Now the codec can respond with either plain text or JSON, depending on the Accept header, without relying on fallback behavior.

abdelfetah18 avatar Jun 10 '25 01:06 abdelfetah18

Deploy Preview for zio-http ready!

Name Link
Latest commit 063acefd5be0c3f5896e11f29d4a8cf85a1bb640
Latest deploy log https://app.netlify.com/projects/zio-http/deploys/684795501a8ec2000878c04a
Deploy Preview https://deploy-preview-3547--zio-http.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

netlify[bot] avatar Jun 10 '25 01:06 netlify[bot]