Http.post cannot handle empty response body #5
Comments
To workaround, evancz/elm-http#5 Unfortunately this workaround consistenly triggers a bug with the Elm task library, https://github.com/elm-lang/core/issues/240
The singleInsert : Task Error Response
singleInsert =
send defaultSettings
{ verb = "POST"
, headers = []
, url = "/table_name"
, body = string """{ "col1": "value1", "col2": "value2" }"""
}
`andThen` handleResponse
handleResponse response =
case Dict.get "Location" response.headers of
Nothing ->
fail (UnexpectedPayload "response headers should have 'Location' field")
Just loc ->
succeed loc There is a bit more to it, but this is how I'd handle this case. The empty string is not valid JSON, so while it'd make your case simpler, it does not actually makes sense for a library about decoding valid JSON strings. |
In terms of option number 1, that can be done too. import Json.Decode as Json
decodeAlways value =
Json.value `Json.andThen` (\_ -> Json.succeed value) So I'm gonna close this one. It may make sense to open an issue on core suggesting |
It's probably worth mentioning to those who arrived here looking for advice on how to handle an empty response body that option 1 doesn't work for that scenario. As noted elsewhere |
I just want to mention for others having this issue with an empty response. I used promoteError from rgrempel/elm-http-decorators as a simple workaround. |
Bumped into this. Can't believe it's 2 years old and it's not properly explained in the docs. I wrote my own
|
The single insert API of postgrest returns an empty response body (actual information, a redirect URL, is in one of the response headers).
This doesn't work well with
Http.post
, because none of the json decoders will successfully parse an empty body. The Json.Decode module doesn't export any function that allows us to bypass empty input:Solutions
Json.Decode.always : a -> Decoder a
and pass it to Http.post (Unlikesucceed
,always
will ignore the input without creating any parse errors).emptyDecoder
in elm-http, and invokeHttp.post Http.emptyDecoder ...
.Any thoughts on this? It won't be ideal for me to fork the implementation of
post
and directly invokesend
and handle errors.The text was updated successfully, but these errors were encountered: