# Fetch

<a href=""><img src="" alt="GitHub release"></a>
<a href=""><img src="" alt="Discord chat"></a>

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.set_method(Get)
    |> request.set_host("")
    |> 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