fabiommendes/elm-request/3.0.1

NOTE: the latest version is 4.0.0

Request

type Request url value

Represent an HTTP request

= GET
| POST
| PUT
| DELETE
| PATCH
| OPTIONS
| HEAD

An enum that represent all valid HTTP methods.

map : (a -> b) -> Request url a -> Request url b

Modify the expected return value for the effect

Config objects

type alias Config url error =
{ toUrl : url -> String
, fromHttpResponse : Response String -> error
, timeout : Maybe Float
, risky : Bool
, headers : List ( String, String )
}

Config objects allow Requests that uses different URL representations than Strings and different error types than Http.Error.

simple : Config String Error

The default config

simpleFromHttpResponse : Response String -> Error

Convert Http.Response to Http.Error

Sometimes it is easier to use this to create custom error types.

Create requests

-> { url : url
, expect : Expect a
}
-> Request url a

Create a simple http request

get :
{ url : url
, expect : Expect a
}
-> Request url a

Create a GET request.

{ url : url
, expect : Expect a
}
-> Request url a

Create a POST request.

put :
{ url : url
, expect : Expect a
}
-> Request url a

Create a PUT request.

{ url : url
, expect : Expect a
}
-> Request url a

Create a DELETE request.

{ url : url
, expect : Expect a
}
-> Request url a

Create a PATCH request.

{ url : url
, expect : Expect a
}
-> Request url a

Create an OPTIONS request.

Declare task content

type Expect value

Represents the expected response in an Http request

expectJson : Decoder a -> Expect a

Declares a request that expects a JSON response handled with the given decoder.

expectBytes : Decoder a -> Expect a

Declares a request that expects a binary response handled with the given decoder.

Declares a request that expects a simple string response.

Ignores the content of the received response.

Convert to commands and tasks

cmd :
Config url error
-> (Result error value -> msg)
-> Request url value
-> Cmd msg

Convert a request to a command.

This function requires a config object. If you don not need one, use the simple config exported by this package.

Request.get
    { url = "https://example.com"
    , expect = Request.expectJson Json.Decode.string
    }
    |> Request.cmd Request.simple ResponseReceived
task : Config url error -> Request url value -> Task error value

Convert a request to a task.

This function requires a config object. If you don not need one, use the simple config exported by this package.

Request.get
    { url = "https://example.com"
    , expect = Request.expectJson Json.Decode.string
    }
    |> Request.task Request.simple

Declares the payload

withJsonBody : Value -> Request url a -> Request url a

Declares a JSON body

withStringBody : String -> String -> Request url a -> Request url a

Declares a string body by specifing the content type and the data.

Request.get
    { url = "https://example.com"
    , expect = Request.expectJson Json.Decode.string
    }
    |> Request.withStringBody "text/plain" "Some text"
withEmptyBody : Request url a -> Request url a

Set the request body to be empty.

withBytesBody : String -> Bytes -> Request url a -> Request url a

Declares a Binary body with a given content type.

withFileBody : Maybe String -> File -> Request url a -> Request url a

Declares a File body with a given file and an optional identifier.

The name can be used to track the download progress of the file by subscribing to events created by Http.tracker id onProgressMsg.

Set request parameters

withBody : Body -> Request url a -> Request url a

Set the request body using some instance of Http.Body

Http.stringBody "text/plain" "Some text"

Requests expose some helper functions, but for more complex cases, you should refer to the Http module.

withHeaders : List ( String, String ) -> Request url a -> Request url a

Adds a list of headers to the request.

Request.get
    { url = "https://example.com"
    , expect = Request.expectJson Json.Decode.string
    }
    |> Request.withHeaders [ ( "Authorization", "Bearer " ++ token ) ]
withQuery : String -> String -> Request url a -> Request url a

Adds a query parameter to the final url

Request.get
    { url = "https://example.com"
    , expect = Request.expectJson Json.Decode.string
    }
    |> Request.withQuery "search" "what is the answer to the question of life, the universe and everything?"
withIntQuery : String -> Int -> Request url a -> Request url a

Adds an integer query parameter to the final url

Request.get
    { url = "https://example.com"
    , expect = Request.expectJson Json.Decode.string
    }
    |> Request.withIntQuery "default-answer" 42

Advanced request parameters

excludeDefaultHeaders : List String -> Request url a -> Request url a

Ignore specific default headers declared in the config.

Request.get
    { url = "https://example.com"
    , expect = Request.expectJson Json.Decode.string
    }
    |> Request.excludeDefaultHeaders [ "Authorization", "Cookie" ]

Ignore all default headers declared in the config.

Request.get
    { url = "https://example.com"
    , expect = Request.expectJson Json.Decode.string
    }
    |> Request.ignoreDefaultHeaders
risky : Bool -> Request url value -> Request url value

Declares the request as risky (or not) according to the boolean argument.

Request.get
    { url = "https://example.com"
    , expect = Request.expectJson Json.Decode.string
    }
    |> Request.risky True