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.
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.
Discard the response body.
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