type alias PostResponse =
  (Http.Response, Int)

type alias Model =
    { error : String
    , response:  String
     }


type Msg
    = CreateSeat
    | PostSucceed PostResponse
    | PostFail Http.Error


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)
   
post' : String -> Http.Body -> Task.Task RawError Http.Response
-- post' decoder url body =
post' url body =
    Http.send Http.defaultSettings
    { verb = "POST"
    , headers =[ ("Content-type", "application/json")]
    , url = url
    , body = body
    }
    -- |> Http.fromJson decoder


promoteError : RawError -> Error
promoteError 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
    Task.mapError promoteError task `Task.andThen` decoded

fakeDecoder : JsonDecode.Decoder Int
fakeDecoder =
    "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

HOWEVER I just get the value field of the response and nothing else

UnexpectedPayload "Expecting an object with a field named `fakeInt` but 
instead got: {\"data\":{\"seatNo\":100,\"occupied\":true,\"id\":67}}"

What's wrong?

-- 
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