This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
Extra functions for more easily building and handling Http requests
version 2.1.0
license MIT
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 2.1.0
Committed At 2016-11-22 17:10:11 UTC
elm-lang/http 1.0.0 <= v < 2.0.0 1.0.0
elm-lang/core 5.0.0 <= v < 6.0.0 5.1.1

Modules

README

elm-http-builder

Build Status

Chainable functions for building HTTP requests and composable functions for handling responses.

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

This is an Elm 0.18 fork of lukewestby's elm-http-builder package that provides an API for specifying Responses as well as Requests.

Example

In this example, we expect a successful response to be JSON array of strings, like:

["hello", "world", "this", "is", "the", "best", "json", "ever"]

and an error response might have a body which just includes text, such as the following for a 404 error:

Not Found.

We'll use HttpBuilder.jsonReader and a Json.Decode.Decoder to parse the successful response body and HttpBuilder.stringReader to accept a string body on error without trying to parse JSON.

import Task exposing (Task)
import Time
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) ]


addItem : String -> Task (HttpBuilder.Error String) (HttpBuilder.Response (List String))
addItem item =
  HttpBuilder.post "http://example.com/api/items"
    |> withJsonBody (itemEncoder item)
    |> withHeader "Content-Type" "application/json"
    |> withTimeout (10 * Time.second)
    |> withCredentials
    |> send (jsonReader itemsDecoder) stringReader