It can be hard to figure out how to deal with all of the type machinery in Servant. This post details adding headers to the response of a Servant API.
Given the following API, which returns NoContent for a GET request.
and the following handler, which assumes we've set up a
custom Monad stack called
App (note that we've skipped the
"server" boilerplate for brevity, since it is covered in the
We can add a response header using addHeader, which is detailed in the docs on Hackage. What may be less obvious is that addHeader changes the response type of the route. Our simple API changes it's type to represent the new return value:
and our handler now can return the appropriate type as well:
If we inspect the type we added to be able to return our
first header, we can notice that it is a list of
We can use this to add multiple new headers to our route as