Fetch
Bindings to JavaScript’s built in HTTP client, fetch
.
import gleam/fetch
import gleam/http.{Get}
import gleam/http/request
import gleam/http/response.{Response}
import gleam/javascript/promise.{try_await}
pub fn main() {
// Prepare a HTTP request record
let req = request.new()
|> request.set_method(Get)
|> request.set_host("test-api.service.hmrc.gov.uk")
|> request.set_path("/hello/world")
|> request.prepend_header("accept", "application/vnd.hmrc.1.0+json")
// Send the HTTP request to the server
use resp <- try_await(fetch.send(req))
use resp <- try_await(fetch.read_text_body(resp))
// We get a response record back
assert Response(status: 200, ..) = resp
assert Ok("application/json") = response.get_header(resp, "content-type")
assert "{\"message\":\"Hello World\"}" = resp.body
promise.resolve(Ok(Nil))
}