6

I want to write API endpoint that would return different http code depending on incoming parameter and state of external world. In python, it would look like following (wsgi):

def application(environ, start_response):
    if check_foo():
        start_response("200 OK", [])
        return b'42'
    else:
        start_response("201 Created", [])
        return b'43'

It is not oblivious how to do it with Servant:

type UserAPI = Verb 'GET 201 '[JSON] Int :<|> Verb 'GET 200 '[JSON] Int

server :: Server UserAPI
server = pure 42 :<|> pure 43

compiles and works, but only returns 42, http code 201. Sure, I can sneak anything I want with ServerError, but this way I lose all advantages of servant, and will have to do serialization myself.

Alternatively, I could push distinction between 200 and 201 into return data, but it is important to me to keep http codes informative.

So, is there way for one endpoint to return different two different http code, with both of them considered "success"?

duplode
  • 33,731
  • 7
  • 79
  • 150
KAction
  • 587
  • 2
  • 10
  • It looks like this is being tackled under [this issue](https://github.com/haskell-servant/servant/issues/841), and someone submitted a draft pull request a couple weeks ago. – K. A. Buhr Jun 25 '20 at 21:15
  • I haven't used it, but [`UVerb`](https://hackage.haskell.org/package/servant-0.19/docs/Servant-API-UVerb.html) seems to do what you want. There's even a cookbook entry for this: . – Taylor Fausak Apr 06 '22 at 20:19

0 Answers0