Skip to content

Instantly share code, notes, and snippets.

@focusaurus
Last active November 7, 2016 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save focusaurus/64973ffb374ea4942f3ce82e685fe728 to your computer and use it in GitHub Desktop.
Save focusaurus/64973ffb374ea4942f3ce82e685fe728 to your computer and use it in GitHub Desktop.
How to handle Http POST
module SignIn exposing (..)
import Html exposing (..)
import Html.App
import Http
import Json.Decode
import Task
type alias Model =
{ signInMessage : String
}
model : Model
model =
{ signInMessage = "Request not sent"
}
type Message
= SignInStart
| SignInSucceed String
| SignInFail Http.Error
view : Model -> Html Message
view model =
label [] [ text model.signInMessage ]
update : Message -> Model -> ( Model, Cmd Message )
update message model =
case message of
SignInStart ->
( { model | signInMessage = "Signing in..." }, Cmd.none )
SignInSucceed _ ->
( { model | signInMessage = "Sign in succeeded" }, Cmd.none )
SignInFail error ->
( { model | signInMessage = "Sign in failed" ++ toString error }, Cmd.none )
init =
( model, Task.perform SignInFail SignInSucceed (Http.post Json.Decode.succeed "sign-in" (Http.string """{"email":"foo@example.com","password":"bar"}""")) )
main : Program Never
main =
Html.App.program
{ init = init
, view = view
, update = update
, subscriptions = (\model -> Sub.none)
}
@focusaurus
Copy link
Author

Compiler error message is:

The 3rd argument to function `perform` is causing a mismatch.

45|              Task.perform SignInFail SignInSucceed (Http.post Json.Decode.value "sign-in" (Http.string """{"email":"foo@example.com","password":"bar"}""")
                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Function `perform` is expecting the 3rd argument to be:

    Task.Task Http.Error String

But it is:

    Task.Task Http.Error Json.Encode.Value

Hint: I always figure out the type of arguments from left to right. If an
argument is acceptable when I check it, I assume it is "correct" in subsequent
checks. So the problem may actually be in how previous arguments interact with
the 3rd.

Detected errors in 1 module.                                        

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment