Gleam httpc

repository·main·Indexed 41 days ago

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

Gleam bindings to Erlang's built-in HTTP client for making HTTP requests within the Erlang ecosystem.

Tokens
550
Snippets
2
Records
3
Agent score
2%

What's inside httpc

  1. Security warning for Erlang/OTP versions older than 26.0

    main

    If you are using Erlang/OTP versions older than 26.0, httpc does not verify TLS connections by default. This means connections to HTTPS endpoints may not be secure.

    To ensure security, you should:

    1. Upgrade to Erlang/OTP 26.0 or newer.
    2. Or, use a different HTTP client such as hackney.
  2. Send an HTTP request with httpc.send

    main

    The httpc library provides bindings to Erlang's built-in HTTP client. To send a request, you typically use gleam/http/request to construct a request record and then pass it to httpc.send/1.

    Note that httpc.send returns a Result containing a response record. You should handle the result (e.g., using result.try) to access the response status, headers, and body.

    import gleam/http/request
    import gleam/http/response
    import gleam/httpc
    import gleam/result
    
    pub fn send_request() {
      // Prepare a HTTP request record
      let assert Ok(base_req) =
        request.to("https://test-api.service.hmrc.gov.uk/hello/world")
    
      let req =
        request.prepend_header(base_req, "accept", "application/vnd.hmrc.1.0+json")
    
      // Send the HTTP request to the server
      use resp <- result.try(httpc.send(req))
    
      // We get a response record back
      assert resp.status == 200
    
      let content_type = response.get_header(resp, "content-type")
      assert content_type == Ok("application/json")
    
      assert resp.body == "{\"message\":\"Hello World\"}"
    
      Ok(resp)
    }