Convenience functions for creating HTTP requests and interpreting an HTTP response.
Helpers for creating HTTP requests.
Convenience function to generate a list of Http.Headers
from a List ( String, String ).
listToHeaders [ ( "Max-Forwards", "10" ), ( "Authorization", "Basic pw123" ) ]
== [ Http.Header "Max-Forwards" "10", Http.Header "Authorization" "Basic pw123" ]Convenience function to generate a percent-encoded
query string from a List ( String, String ).
listToQuery [ ( "foo", "abc 123" ), ( "bar", "xyz" ) ]
== "?foo=abc%20123&bar=xyz"Note: A more appropriate place for this function would probably be a package like Url.Extras.
However, such a package doesn't exist, and I use this function quite frequently, so I've included it here.
Helpers for interpreting an Http.Response value.
Transform an Http.Response value into the respective Result that is returned
in each expect function from elm/http. Used for building custom expect functions.
For example, you could replicate Http.expectString like so:
import Http
import Http.Extras
expectString : (Result Http.Error String -> msg) -> Http.Expect msg
expectString toMsg =
Http.expectStringResponse toMsg Http.Extras.responseToStringConvenience functions for extracting information like the header, status code, url, etc. from a Http.Response value.
On an error, a short string will be returned describing why the error occurred. For example:
getStatusCode Http.Timeout_
== Err "Timeout"These functions are primarily concerned with accessing the metadata of a response.
So, these functions will return a successful Result if the response is GoodStatus_ or BadStatus_.
Otherwise, the Result will be an error.
Note that this only tries to return the url from the metadata - it does not return the url if
the response is BadUrl_
Returns the status code if 200 <= status code < 300
A custom type for keeping track of the state of a HTTP requests in your program's Model. This is just a suggested pattern to use in your development. It's included primarily for my own convenience - here's an example of how it would be used.
type alias Model =
{ apiCats : State ( CatsResponseType, Http.Metadata ) (Http.Detailed.Error String)
}
type Msg =
APICatsResponse
| ...
init =
( { apiCats = NotRequested }, Cmd.none )
update msg model =
case msg of
APICatsResponse httpResponse ->
case httpResponse of
Ok (response, metadata) ->
( { model | apiCats = Success (response, metadata) }
, Cmd.none )
Err httpError ->
( { model | apiCats = Error httpError }
, Cmd.none )
...
The type that needs to be passed into
Http.request. It's never actually defined as a type inelm/http, so this is just the type definition for it.