Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lukewestby/elm-http-builder
elm-http-builder
https://github.com/lukewestby/elm-http-builder
elm http
Last synced: 12 days ago
JSON representation
elm-http-builder
- Host: GitHub
- URL: https://github.com/lukewestby/elm-http-builder
- Owner: lukewestby
- License: mit
- Created: 2016-05-01T17:55:35.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2020-12-09T16:54:47.000Z (almost 4 years ago)
- Last Synced: 2024-10-26T18:29:58.594Z (15 days ago)
- Topics: elm, http
- Language: Elm
- Homepage: http://package.elm-lang.org/packages/lukewestby/elm-http-builder/latest
- Size: 171 KB
- Stars: 162
- Watchers: 5
- Forks: 19
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# elm-http-builder
Pipeable functions for building HTTP requests
```elm
import Http
import HttpBuilder
import Json.Decode as Decode
import Json.Encode as Encode
import Url.Builder as UrlBuildertype Status a = Loading | Loaded a | Failure
type alias Model = { items : Status (List String) }
itemsDecoder : Decode.Decoder (List String)
itemsDecoder =
Decode.list Decode.stringitemEncoder : String -> Encode.Value
itemEncoder item =
Encode.object
[ ("item", Encode.string item) ]{-| addItem will send a post request to
`"http://example.com/api/items?hello=world"` with the given JSON body and a
custom header. It'll try to decode with `itemsDecoder`.-}
addItem : String -> Cmd Msg
addItem item =
UrlBuilder.crossOrigin
"http://example.com"
[ "api", "items" ]
[ UrlBuilder.string "hello" "world" ]
|> HttpBuilder.post
|> HttpBuilder.withHeader "X-My-Header" "Some Header Value"
|> HttpBuilder.withJsonBody (itemEncoder item)
|> HttpBuilder.withTimeout 10000
|> HttpBuilder.withExpect (Http.expectJson GotItem itemsDecoder)
|> HttpBuilder.requesttype Msg = GotItem (Result Http.Error (List String))
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GotItem (Ok items) ->
( { model | items = Loaded items }
, Cmd.none
)GotItem (Err err) ->
( { model | items = Failure } , Cmd.none)```