Http response Error BadStatus no body

I have a backend that uses error codes with a json body containing extra information for expected events.
So I wanted to implement those cases into my frontend, but noticed that the according error in elms http module does not contain information about the body (just about the status code), is there any reason for not including the body I’m missing?
I can make changes to my backend or use some ports or something to make my own http thing for now, but I just wanted to ask whats the reason for that .

1 Like

Hey :slight_smile:.
If I understand you correctly you can achieve this with the

expectStringResponse : 
    (Result x a -> msg)
    -> (Response String -> Result x a)
    -> Expect msg

function. The documentation in elm/http specifically mentions your use case.
With most of the basic expect functions you get back a

Result Http.Error a

after your http request, but here you can provide a function that turns a Response String into a Result x a where x could be any error type you want, maybe (Http.Error, String) or something more fancy. And with Response String you have access to the body of the http response to get more precise information on the error.

Hope this helps. :slight_smile:

1 Like

This can also come in handy: Http.Detailed - http-extras 2.1.0


The question is already answered, but as an aside: I’ve checked the HTTP specification again and it says:

Except when responding to a HEAD request, the server SHOULD send a representation containing an explanation of the error situation, […] User agents SHOULD display any included representation to the user.

Therefore, I would argue that Elm should include the body in the BadStatus error by default.


This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.