Get updates via , Github, or RSS | About

Episode 38: HttpBuilder Jun 15, 2017

Building an HTTP request can get messy if you include extra headers, decode the body into something other than Json, or even append some query parameters. HttpBuilder works with sane defaults and simple functions to help you construct your requests with minimal effort.

Examples

Main.elm

case msg of
    Signin ->
        let
            builderCmd =
                HttpBuilder.post "http://localhost:4000/signin"
                    |> HttpBuilder.withHeader "X-CSRF-TOKEN" model.token
                    |> HttpBuilder.withJsonBody jsonBody
                    |> HttpBuilder.withQueryParams [ ( "timestamp", (toString model.timestamp) ) ]
                    |> HttpBuilder.withExpect (Http.expectJson Decode.int)
                    |> HttpBuilder.send SigninCompleted
        in
            model ! [ builderCmd ]