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 "@andrewmacmurray/elm-concurrent-task"
import { HttpRequest, HttpResponse } from "@andrewmacmurray/elm-concurrent-task"
ConcurrentTask.register({
tasks: {},
ports: app.ports,
builtins: {
http: customRequest
},
});
function customRequest(req: HttpRequest): Promise<HttpResponse> {
return ...<Your Custom Http Request>
}See the typescript definitions and the fetch adapter to see how to create your own.
Note:
You're not required to use this module for http requests in ConcurrentTask, it's here for convenience.
You could create entirely your own from scratch - maybe you want an http package with request caching or special retry logic built in on the JS side.
Send an Http GET request
Send an Http POST request
Send data in your http request.
Create an empty body for your request.
This is useful for GET requests and POST requests where you are not sending any data.
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.
Put some JSON value in the body of your request. This will automatically add the Content-Type: application/json header.
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" bytesThe 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.
Expect the response body to be JSON, decode it using the supplied decoder.
Expect the response body to be a String.
Expect the response body to be Bytes, decode it using the supplied decoder.
Discard the response body.
Include Http metadata in a successful response.
An Http header for configuring a request.
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.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.
Send an Http request - similar to
elm/http'sHttp.Task