lue-bird / elm-state-interface / Web.Http

Helpers for HTTP types as part of an Interface

request : Web.HttpRequest future -> Web.Interface future

An Interface for handling an HttpRequest using the fetch API

get : { url : String, expect : Web.HttpExpect future } -> Web.HttpRequest future

Create a GET HttpRequest.

Use Web.Http.addHeaders to set custom headers as needed. Use Web.Time.onceAt to add a timeout of how long you are willing to wait before giving up.

post : { url : String, body : Web.HttpBody, expect : Web.HttpExpect future } -> Web.HttpRequest future

Create a POST HttpRequest.

Use Web.Http.addHeaders to set custom headers as needed. Use Web.Time.onceAt to add a timeout of how long you are willing to wait before giving up.

addHeaders : List ( String, String ) -> Web.HttpRequest future -> Web.HttpRequest future

Add custom headers to the HttpRequest.

request
    |> Web.Http.addHeaders
        [ ( "X-Custom-Header", "ProcessThisImmediately" )
        ]

expectString : Web.HttpExpect (Result Web.HttpError String)

Expect the response body to be a String.

expectJson : Json.Decode.Decoder future -> Web.HttpExpect (Result Web.HttpError (Result { actualBody : String, jsonError : Json.Decode.Error } future))

Expect the response body to be JSON, decode it using the given decoder. The result will either be

expectBytes : Web.HttpExpect (Result Web.HttpError Bytes)

Expect the response body to be Bytes. The result will either be

expectWhatever : Web.HttpExpect (Result Web.HttpError ())

Discard the response body.

bodyJson : Json.Encode.Value -> Web.HttpBody

Put a given JSON value in the body of your request. This will automatically add the Content-Type: application/json header.

bodyBytes : String -> Bytes -> Web.HttpBody

Put given Bytes in the body of your request. The string argument should be a MIME type to be used in the Content-Type header

import Bytes exposing (Bytes)
import Bytes.Encode
import Time
import Web
import Zip
import Zip.Entry

exampleZipBody : Web.HttpBody
exampleZipBody =
    Web.Http.bodyBytes "application/zip"
        (Zip.fromEntries
            [ Bytes.Encode.string "Hello, World!"
                |> Bytes.Encode.encode
                |> Zip.Entry.store
                    { path = "hello.txt"
                    , lastModified = ( Time.utc, Time.millisToPosix 0 )
                    , comment = Nothing
                    }
            ]
            |> Zip.toBytes
        )