Extra helpers for more easily building Http requests that require greater
configuration than what is provided by elm/http
out of the box.
{ method : String
, headers : List Http.Header
, url : String
, body : Http.Body
, resolver : Http.Resolver x a
, timeout : Maybe Basics.Float
, withCredentials : Basics.Bool
}
A type for chaining request configuration
get : String -> RequestBuilder x ()
Start building a GET request with a given URL
get "https://example.com/api/items/1"
post : String -> RequestBuilder x ()
Start building a POST request with a given URL
post "https://example.com/api/items"
put : String -> RequestBuilder x ()
Start building a PUT request with a given URL
put "https://example.com/api/items/1"
patch : String -> RequestBuilder x ()
Start building a PATCH request with a given URL
patch "https://example.com/api/items/1"
delete : String -> RequestBuilder x ()
Start building a DELETE request with a given URL
delete "https://example.com/api/items/1"
options : String -> RequestBuilder x ()
Start building a OPTIONS request with a given URL
options "https://example.com/api/items/1"
trace : String -> RequestBuilder x ()
Start building a TRACE request with a given URL
trace "https://example.com/api/items/1"
head : String -> RequestBuilder x ()
Start building a HEAD request with a given URL
head "https://example.com/api/items/1"
withHeader : String -> String -> RequestBuilder x a -> RequestBuilder x a
Add a single header to a request
get "https://example.com/api/items/1"
|> withHeader "Content-Type" "application/json"
withHeaders : List ( String, String ) -> RequestBuilder x a -> RequestBuilder x a
Add many headers to a request
get "https://example.com/api/items/1"
|> withHeaders [ ( "Content-Type", "application/json" ), ( "Accept", "application/json" ) ]
withBody : Http.Body -> RequestBuilder x a -> RequestBuilder x a
Add an Http.Body to the request
post "https://example.com/api/save-text"
|> withBody (Http.stringBody "text/plain" "Hello!")
withStringBody : String -> String -> RequestBuilder x a -> RequestBuilder x a
Convenience function for adding a string body to a request
post "https://example.com/api/items/1"
|> withStringBody "application/json" """{ "sortBy": "coolness", "take": 10 }"""
withJsonBody : Json.Encode.Value -> RequestBuilder x a -> RequestBuilder x a
Convenience function for adding a JSON body to a request
params = Json.Encode.object
[ ("sortBy", Json.Encode.string "coolness")
, ("take", Json.Encode.int 10)
]
post "https://example.com/api/items/1"
|> withJsonBody params
withMultipartStringBody : List ( String, String ) -> RequestBuilder x a -> RequestBuilder x a
Convenience function for adding multipart bodies composed of String, String
key-value pairs. Since Http.stringData
is currently the only Http.Data
creator having this function removes the need to use the Http.Data
type in
your type signatures.
post "https://example.com/api/items/1"
|> withMultipartStringBody [ ( "user", JS.encode user ) ]
withUrlEncodedBody : List ( String, String ) -> RequestBuilder x a -> RequestBuilder x a
Convenience function for adding url encoded bodies
post "https://example.com/api/whatever"
|> withUrlEncodedBody [ ( "user", "Luke" ), ( "pwd", "secret" ) ]
withTimeout : Basics.Float -> RequestBuilder x a -> RequestBuilder x a
Set the timeout
setting on the request
get "https://example.com/api/items/1"
|> withTimeout (10 * Time.second)
withCredentials : RequestBuilder x a -> RequestBuilder x a
Set the withCredentials
flag on the request to True. Works via
XMLHttpRequest#withCredentials
get "https://example.com/api/items/1"
|> withCredentials
withResolver : Http.Resolver y b -> RequestBuilder x a -> RequestBuilder y b
Choose a Resolver
for the request
get "https://example.com/api/items/1"
|> withResolver (Http.stringResolver toResult)
withBearerToken : String -> RequestBuilder x a -> RequestBuilder x a
Add a bearer token to a request
get "https://example.com/api/items/1"
|> withBearerToken "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYSJ9.MvhYYpYBuN1rUaV0GGnQGvr889zY0xSc20Lnt8nMTfE"
toTask : RequestBuilder x a -> Task x a
Send the request