GitHub release Discord chat test

Bindings to JavaScript’s built in HTTP client, fetch.

import gleam/fetch
import gleam/http.{Get}
import gleam/http/request
import gleam/http/response
import gleam/javascript/promise.{try_await}

pub fn main() {
  // Prepare a HTTP request record
  let request = 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 response <- try_await(fetch.send(request))
  use response <- try_await(fetch.read_text_body(response))

  // We get a response record back
  assert Response(status: 200, ..) = response

  assert Ok("text/html; charset=utf-8") =
    http.get_resp_header(resp, "content-type")

  assert "{\"message\":\"Hello World\"}" = resp.body

Search Document