requests - HTTP Client Library for OCaml#
A modern HTTP(S) client library for OCaml with Eio support, providing a clean API for making web requests with automatic TLS/CA certificate handling. Inspired by Python's requests library.
Key Features#
- Clean Eio-style API: Async I/O using OCaml 5's Eio library
- Automatic TLS: Built-in TLS support with automatic CA certificate handling
- Simple Interface: Intuitive API similar to Python's requests library
- Type-safe: Leverages OCaml's type system for safer HTTP operations
- Comprehensive: Support for common HTTP methods, headers, authentication, retries, and timeouts
Usage#
Basic GET request:
let () =
Eio_main.run @@ fun env ->
let response = Requests.get ~env (Uri.of_string "https://example.com") in
print_endline (Requests.Response.body_string response)
POST request with JSON body:
let () =
Eio_main.run @@ fun env ->
let uri = Uri.of_string "https://api.example.com/data" in
let body = {|{"key": "value"}|} in
let headers = Requests.Headers.of_list [
("content-type", "application/json")
] in
let response = Requests.post ~env ~headers ~body uri in
Printf.printf "Status: %d\n" (Requests.Response.status_code response)
Using authentication and custom headers:
let () =
Eio_main.run @@ fun env ->
let auth = Requests.Auth.basic ~username:"user" ~password:"pass" in
let headers = Requests.Headers.add
(Requests.Headers.init ())
"user-agent" "my-app/1.0" in
let response = Requests.get ~env ~auth ~headers
(Uri.of_string "https://api.example.com") in
(* Process response *)
()
Installation#
opam install requests
Documentation#
API documentation is available at https://tangled.org/@anil.recoil.org/ocaml-requests or via:
opam install requests
odig doc requests
License#
ISC