haskell-servant / haskell-servant/servant

Wildcard media type in `Content-Type` response header

Open
#1,275 0 comments 3 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
2k
Forks
427
Avg merge
2d 23h
Merged PRs (30d)
5

Description

In the following example, I try to override the `Content-Type` header in Handler:

``` Haskell
data IMAGE deriving Typeable

instance Accept IMAGE where
contentType _ = "image" // "*"

type API = Capture "id" Int :> Get '[IMAGE] (Headers '[Header "Content-Type" String] ByteString)

server :: Server API
server id = do
let ctype = renderHeader $ "image" // "jpeg"
file = "/www/images/" (show id) <.>"jpeg"
return $ addHeader ctype $ readFile file
```

I send the request `http://localhost/123` in browser, but got two `Content-Type` headers in the response:

```
Content-Type: image/*
Content-Type: image/jpeg
```

I think the problem is that the servant requires the client to set the `Accept` header to explicitly specify the desired media type (otherwise it will fall back to `* / *`), but the servant cannot correctly resolve the wildcard media type, it just sends back the `Content-Type: image / *` (declared by `IMAGE`) when the client requests with `Accept: image / *`.

My recomendation is that servant should not set wildcard media type header in response, just let users dynamically set the `Content-Type` header in their `Handler` themselves.

There is a simple implemation of my proposal #1274, may someone help create a full implemation based on this? Thanks a lot!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.