lue-bird/elm-state-interface/2.0.0

NOTE: the latest version is 2.0.4

Web.Http

Helpers for HTTP types as part of an Interface

request : HttpRequest future -> Interface future

An Interface for handling an HttpRequest using the fetch API

get :
{ url : String
, expect : HttpExpect future
}
-> 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.

{ url : String
, body : HttpBody
, expect : HttpExpect future
}
-> 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 ) -> HttpRequest future -> HttpRequest future

Add custom headers to the HttpRequest.

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

Expect the response body to be a String.

Decoder future
(Result
(Result
{ actualBody : String
, jsonError : Error
}
future
)
)

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

  • Err with an HttpError if it didn't succeed
  • Ok if there was a result with either

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

  • Err with an HttpError if it didn't succeed
  • Ok with the Bytes

Discard the response body.

bodyJson : Value -> 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 -> 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
        )