Make concurrent http requests.
The JavaScript runner has this task builtin by default. Internally It uses the fetch api which is widely supported in the Browser and (as of Node 18) in NodeJS.
If needed you can supply a custom implementation like so:
import * as ConcurrentTask from "
request : { url : String, method : String, headers : List Header, body : Body, expect : Expect a, timeout : Maybe Basics.Int } -> ConcurrentTask Error a
Send an Http request - similar to elm/http
's Http.Task
get : { url : String, headers : List Header, expect : Expect a, timeout : Maybe Basics.Int } -> ConcurrentTask Error a
Send an Http GET
request
post : { url : String, headers : List Header, body : Body, expect : Expect a, timeout : Maybe Basics.Int } -> ConcurrentTask Error a
Send an Http POST
request
Send data in your http request.
emptyBody : Body
Create an empty body for your request.
This is useful for GET
requests and POST
requests where you are not sending any data.
stringBody : String -> String -> Body
Put a String
in the body of your request. Defining jsonBody
looks like this:
import Json.Encode as Encode
jsonBody : Encode.Value -> Body
jsonBody value =
stringBody "application/json" (Encode.encode 0 value)
The first argument is a MIME type of the body.
jsonBody : Json.Encode.Value -> Body
Put some JSON value in the body of your request. This will automatically add the Content-Type: application/json
header.
bytesBody : String -> Bytes -> Body
Put some Bytes
in the body of your Request
. This allows you to use
elm/bytes
to have full control over the binary
representation of the data you are sending. For example, you could create an
archive.zip
file and send it along like this:
import Bytes exposing (Bytes)
zipBody : Bytes -> Body
zipBody bytes =
bytesBody "application/zip" bytes
The first argument is a MIME type
of the body. In other scenarios you may want to use MIME types like image/png
or image/jpeg
instead.
NOTE: Because Bytes
can't be sent out of a port (internally ConcurrentTask
sends all its arguments out of a port), they are serialised to a base64 encoded String
.
Unfortunately some of the performance benefits of Bytes
are lost at this point.
Describe what you expect to be returned in an http response body.
expectJson : Json.Decode.Decoder a -> Expect a
Expect the response body to be JSON
, decode it using the supplied decoder.
expectString : Expect String
Expect the response body to be a String
.
expectBytes : Bytes.Decode.Decoder a -> Expect a
Expect the response body to be Bytes
, decode it using the supplied decoder.
expectWhatever : Expect ()
Discard the response body.
withMetadata : (Metadata -> a -> b) -> Expect a -> Expect b
Include Http metadata in a successful response.
( String, String )
An Http header for configuring a request.
header : String -> String -> Header
Create a Header
. e.g.:
header "X-Requested-With" "Fetch"
A Request can fail in a couple ways:
BadUrl
means you did not provide a valid URL.Timeout
means it took too long to get a response.NetworkError
means the user turned off their wifi, went in a cave, etc.BadStatus
means you got a response back, but the status code indicates failure. Contains:Metadata
.Json.Decode.Value
.BadBody
means you got a response back with a nice status code, but the body of the response was something unexpected. Contains:Metadata
.Json.Decode.Value
.Json.Decode.Error
that caused the error.{ url : String
, statusCode : Basics.Int
, statusText : String
, headers : Dict String String
}
Extra information about the response:
Note:
It is possible for a response to have the same header multiple times. In that case, all the values end up in a single entry in the headers dictionary. The values are separated by commas, following the rules outlined here.