Module
pooka/httpc
src/pooka/httpc.pookaAn HTTP client, using the one that comes with Erlang.
import httpc
main() {
match httpc.get("https://example.org/") {
Ok(reply) -> echo reply.status
Error(problem) -> echo httpc.describe(problem)
}
}
Requests and responses are the ones from pooka/http, the same a server
receives. TLS certificates are checked against the operating
system's trusted authorities, and connections are kept open and reused.
API reference
class Options
How a request is sent. Start with defaults, then adjust.
let timeout: Int
Intlet connect_timeout: Int
Intlet follow_redirects: Bool
Boollet verify_tls: Bool
Boolnew(
timeout: Int,
connect_timeout: Int,
follow_redirects: Bool,
verify_tls: Bool
)
fn(Int, Int, Bool, Bool) -> pooka/httpc.Optionswith_timeout(milliseconds: Int) -> Options
fn(Int) -> pooka/httpc.OptionsHow long to wait for the whole response, in milliseconds.
with_connect_timeout(milliseconds: Int) -> Options
fn(Int) -> pooka/httpc.OptionsHow long to wait for the connection, in milliseconds.
following_redirects() -> Options
fn() -> pooka/httpc.OptionsFollow redirects to their end, instead of returning the redirect.
without_tls_verification() -> Options
fn() -> pooka/httpc.OptionsAccept any certificate. Only for servers you control, such as a local one with a self-signed certificate.
defaults() -> Options
fn() -> pooka/httpc.OptionsThirty seconds for a response, ten to connect, no redirects followed, and certificates checked.
class HttpcError
Why a request got no response.
class BadUrl(reason: String) is HttpcError
The URL could not be read.
reason: String
String
fn(String) -> pooka/httpc.BadUrlclass CouldNotConnect(reason: String) is HttpcError
No connection: the host is unknown, refused, or the certificate is not trusted.
reason: String
String
fn(String) -> pooka/httpc.CouldNotConnectclass TimedOut is HttpcError
The connection or the response took too long.
class Unsupported(reason: String) is HttpcError
A method this client cannot send.
reason: String
String
fn(String) -> pooka/httpc.Unsupportedclass Failed(reason: String) is HttpcError
Anything else that went wrong on the way.
reason: String
String
fn(String) -> pooka/httpc.Faileddescribe(problem: HttpcError) -> String
fn(pooka/httpc.HttpcError) -> StringA sentence about what went wrong.
send(request: Request) -> Result(Response, HttpcError)
fn($package/pooka_http/pooka/http.Request) -> Result($package/pooka_http/pooka/http.Response, pooka/httpc.HttpcError)Send a request, and wait for the response. Any status counts as a
response, 404 and 500 included; check reply.status.
send_with(request: Request, options: Options) -> Result(Response, HttpcError)
fn($package/pooka_http/pooka/http.Request, pooka/httpc.Options) -> Result($package/pooka_http/pooka/http.Response, pooka/httpc.HttpcError)Send a request with your own options.
get(url: String) -> Result(Response, HttpcError)
fn(String) -> Result($package/pooka_http/pooka/http.Response, pooka/httpc.HttpcError)GET a URL.
post_text(url: String, body: String) -> Result(Response, HttpcError)
fn(String, String) -> Result($package/pooka_http/pooka/http.Response, pooka/httpc.HttpcError)POST text to a URL.
post_json(url: String, body: String) -> Result(Response, HttpcError)
fn(String, String) -> Result($package/pooka_http/pooka/http.Response, pooka/httpc.HttpcError)POST JSON text, already encoded, to a URL.