Bindings to Erlang's hackney
HTTP client.
Most the time gleam_httpc
is a better
choice as it uses the built-in Erlang HTTP client, but this package may be
useful in some specific situations.
gleam add gleam_hackney@1
import gleam/result
import gleam/hackney
import gleam/http/request
import gleam/http/response
import gleeunit/should
pub fn main() {
// Prepare a HTTP request record
let assert Ok(request) =
request.to("https://test-api.service.hmrc.gov.uk/hello/world")
// Send the HTTP request to the server
use response <- result.try(
request
|> request.prepend_header("accept", "application/vnd.hmrc.1.0+json")
|> hackney.send
)
// We get a response record back
assert response.status == 200
assert response.get_header(response, "content-type")
== Ok("application/json")
assert response.body
== "{\"message\":\"Hello World\"}")
}