Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gleam-lang/fetch

📡 Make requests to HTTP servers with fetch
https://github.com/gleam-lang/fetch

Last synced: about 2 months ago
JSON representation

📡 Make requests to HTTP servers with fetch

Awesome Lists containing this project

README

        

# Fetch

GitHub release
Discord chat
![test](https://github.com/gleam-lang/fetch/workflows/test/badge.svg?branch=main)

Bindings to JavaScript's built in HTTP client, `fetch`.

If you are running your Gleam project on the Erlang target (the default
for new Gleam projects) then you will want to use a different library
which can run on Erlang, such as [gleam_httpc](https://github.com/gleam-lang/httpc).

```gleam
import gleam/fetch
import gleam/http/request
import gleam/http/response
import gleam/javascript/promise

pub fn main() {
let assert Ok(req) = request.to("https://example.com")

// Send the HTTP request to the server
use resp <- promise.try_await(fetch.send(req))
use resp <- promise.try_await(fetch.read_text_body(resp))

// We get a response record back
resp.status
// -> 200

response.get_header(resp, "content-type")
// -> Ok("text/html; charset=UTF-8")

promise.resolve(Ok(Nil))
}
```