I'm getting the following error:

BadPayload "Expecting an object with a field named `id` but instead got: {}" 
{ status = { code = 200, message = "OK" }, headers = Dict.fromList [(
"Cache-Control","no-cache"),("Content-Type","application/json; 
charset=utf-8"),("Expires","-1"),("Pragma","no-cache")], url = 
"http://localhost:4000/players/9";, body = "{}" }

How should I structure my Http.request to expect an empty body?

On Sunday, March 5, 2017 at 11:50:42 PM UTC-8, Brian Hicks wrote:
>
> What error are you getting? It looks like you're asking Elm to decode a 
> player when you know you're not going to be getting one.
>
> On Mar 6, 2017, at 12:19 AM, Long Nguyen <[email protected] <javascript:>> 
> wrote:
>
> Hello,
>
> I have the following helper functions to trigger the DELETE request of a 
> resource.
>
> deletePlayer : Player -> Cmd Msg
> deletePlayer player =
>     restRequest (hostUrl ++ "/players/" ++ (toString player.id))
>         (playerEncode player)
>         playerDecoder
>         DELETE
>         |> Http.send (PlayerMsg_ << OnDeletePlayer)
>
> restRequest : String -> JE.Value -> JD.Decoder a -> HTTPMethod -> 
> Http.Request a
> restRequest url encode decoder method =
>     Http.request
>         { body = encode |> Http.jsonBody
>         , expect = Http.expectJson decoder
>         , headers = []
>         , method = toString method
>         , timeout = Nothing
>         , url = url
>         , withCredentials = False
>         }
>
> And in my Update method I have the following code.
>
> case msg of
>         ....
>         DeletePlayer id ->
>             let
>                 maybePlayer =
>                     playersModel.players
>                         |> List.filter (\player -> player.id == id)
>                         |> List.head
>             in
>                 case maybePlayer of
>                     Just player ->
>                             ( { playersModel | idToDelete = player.id }
>                             , API.deletePlayer player )
>
>                     Nothing ->
>                         ( playersModel, Cmd.none )
>
>         OnDeletePlayer (Ok deletedPlayer) ->
>             Debug.log ("Ok")
>                 ( playersModel, Cmd.none)
>
>         OnDeletePlayer (Err error) ->
>             Debug.log ("Error")
>                 ( playersModel, Cmd.none )
>     
>
> The HTTP request got executed, and the resource gets deleted; however, the 
> problem is that Elm always trigger the "OnDeletePlayer (Err error)" branch 
> of the Update function. I'm using JSON Server 
> <https://github.com/typicode/json-server>, and after the request is 
> executed, the response body is "{}" with 200 as Status Code.
>
> Please help me in structuring my Http.request so that the "OnDeletePlayer 
> (Ok deletedPlayer)" branch gets executed upon success.
>
> Much appreciated!
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] <javascript:>.
> For more options, visit https://groups.google.com/d/optout.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to