Clone or download
Permalink
Failed to load latest commit information.
src upgrade for 0.19 rc1 Aug 21, 2018
.gitignore upgrade for 0.19 rc1 Aug 21, 2018
CHANGELOG.md changelog Mar 8, 2018
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md Feb 16, 2016
LICENSE Initial commit Jan 23, 2016
README.md more readme Aug 21, 2018
elm.json update the readme Aug 21, 2018

README.md

elm-http-builder

Chainable functions for building HTTP requests

Need help? Join the #http-builder channel in the Elm Slack!

import Http
import HttpBuilder exposing (..)
import Json.Decode as Decode
import Json.Encode as Encode


itemsDecoder : Decode.Decoder (List String)
itemsDecoder =
    Decode.list Decode.string


itemEncoder : String -> Encode.Value
itemEncoder item =
    Encode.object
        [ ("item", Encode.string item) ]


handleRequestComplete : Result Http.Error (List String) -> Msg
handleRequestComplete result =
    -- Handle the result

{-| addItem will send a post request to
`"http://example.com/api/items?hello=world"` with the given JSON body, a
custom header, and cookies included. It'll try to decode with `itemsDecoder`.
-}
addItem : String -> Cmd Msg
addItem item =
    HttpBuilder.post "http://example.com/api/items"
        |> withQueryParams [ ("hello", "world") ]
        |> withHeader "X-My-Header" "Some Header Value"
        |> withJsonBody (itemEncoder item)
        |> withTimeout 10000
        |> withExpectJson itemsDecoder
        |> withCredentials
        |> send handleRequestComplete