Fetch
An HTTP-client, built on the Fetch-standard using Lwt for its Promise-implementation.
The entry point of this library is the module: Fetch
.
Examples
Basic request
The following example performs a GET-request.
Fetch.get("https://example.com") == Lwt.t(result(Fetch.Response.t, string));
We could also use the following example to perform a `GET-request since Fetch.fetch
defaults to a `GET-request. But prefer the former since it's more explicit in showing intent.
Fetch.fetch("https://example.com") == Lwt.t(result(Fetch.Response.t, string));
POST-Request
Here's an example that's a bit more involved. We're not using any of the destructured fields from Fetch.Response.t
, but it's important to know that they're available.
let result =
Fetch.(
{
let.flatMapOk {Response.body, headers as _headers, status as _status, url as _url} =
post(
"https://example.com/movies",
~body="Some, body",
~headers=[("Authorization", "Bearer xyz")],
);
let bodyString = Body.toString(body);
Lwt.return(Ok(bodyString));
}
);
switch (result) {
| Ok(bodyString) => ...
| Error(errorMessage) => ...
};
For more examples, have a look in the examples folder
Installing
To install, add the package to your esy-manifest like so:
"dependencies": {
/* other dependencies */
"fetch-native-lwt": "lessp/fetch:fetch-native-lwt.json"
}
And add it to your `dune`-stanza.
(libraries ... fetch-native-lwt)
In your terminal, run: `esy`.