post' : String -> Http.Body -> Task.Task RawError Http.Responsepost' url 
body =    Http.send Http.defaultSettings    { verb = "POST"    , headers =[ 
("Content-type", "application/json")]    , url = url    , body = body    }

promoteError : RawError -> ErrorpromoteError rawError =  case rawError of  
Http.RawTimeout      -> Http.Timeout  Http.RawNetworkError -> 
Http.NetworkError
withResponse : JsonDecode.Decoder a -> Task.Task Http.RawError 
Http.Response -> Task.Task Http.Error (Http.Response, a)withResponse 
decoder task =  let    decoded response =      Task.map (\val -> (response, 
val)) (Http.fromJson decoder (Task.succeed response))  in    Task.mapError 
promoteError task `Task.andThen` decoded


-- a FAKE decoder to get the UnexpectedPayload message :)
fakeDecoder : JsonDecode.Decoder IntfakeDecoder =    "fakeInt"  := ( 
JsonDecode.int )


postCommand : Cmd Msg
postCommand =
  let
    body = Http.string """{"seat":{"seat_no": 100, "occupied": true}}"""
    url = "http://localhost:4000/api/seats";

  in
    post' url body
    |> withResponse fakeDecoder
    |> Task.perform PostFail PostSucceed

type alias MyResponse =
  (Http.Response, Int)


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
    case msg of
 CreateSeat ->
      (model, postCommand )

  PostSucceed  response ->
      ( { model | response = (toString response) },  Cmd.none )
 
  PostFail errorMessage ->
      ( {  model | error = (toString errorMessage) }, Cmd.none)


And then I got the same result, the value of the Http.Response only :
UnexpectedPayload "Expecting an object with a field named `seatNo` but 
instead got: {\"data\":{\"seatNo\":100,\"occupied\":true,\"id\":62}}"

What's wrong?


Le jeudi 16 juin 2016 01:10:02 UTC+8, William Casarin a écrit :
>
> Hey germain, 
>
> On Wed, Jun 15, 2016 at 8:51 AM, germain <germai...@gmail.com 
> <javascript:>> wrote: 
> > Hello, 
> > [..] 
> > Is it possible to retrieve the whole response, for example to retrieve 
> the 
> > status and the value fields? 
>
> So this is a bit ugly because promoteError is not exposed, but it can 
> be accomplished with `Task.andThen`: 
>
>
>     promoteError : RawError -> Error 
>     promoteError rawError = case rawError of 
>                               RawTimeout      -> Timeout 
>                               RawNetworkError -> NetworkError 
>
>     withResponse : Decoder a -> Task RawError Response -> Task Error 
> (Response, a) 
>     withResponse decoder task = 
>       let 
>           decoded resp = Task.map (\val -> (resp, val)) 
>                                   (Http.fromJson decoder (Task.succeed 
> resp)) 
>       in 
>           Task.mapError promoteError task `Task.andThen` decoded 
>
>
> Cheers, 
> William 
>
> --- 
> https://jb55.com 
>

-- 
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 elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to