Hcs.Http
hcs · API reference
HTTP request builder DSL.
Use this module to construct client requests with methods, headers, cookies, query parameters, body strings, JSON bodies, and forms before executing them with Client.
HTTP Request Builder DSL.
This module provides a fluent API for building HTTP requests. It's designed to be used with the Client module for a high-level HTTP client experience.
Usage
open Hcs.Http
(* Simple GET request *)
let req = get (Uri.of_string "https://api.example.com/users") |> build
(* POST with JSON body *)
let req =
post (Uri.of_string "https://api.example.com/users")
|> content_type "application/json"
|> body_string {|{"name": "Alice"}|}
|> build
(* GET with query params and auth *)
let req =
get (Uri.of_string "https://api.example.com/search")
|> query "q" "ocaml" |> query "limit" "10" |> bearer "my-token" |> buildTypes
type meth =
| GET
| POST
| PUT
| DELETE
| PATCH
| HEAD
| OPTIONS
| CONNECT
| TRACE
| Other of stringHTTP method
type body =
| Empty
| String of string
| Form of (string * string) listBody content
type builder = {
meth : meth;
uri : Uri.t;
headers : (string * string) list;
body : body;
}Request builder - accumulates request parameters
type request = {
req_meth : meth;
req_uri : Uri.t;
req_headers : (string * string) list;
req_body : body;
}Built request ready for execution
Method to Hcs.Method conversion
Maps the request-builder method to hcs's polymorphic-variant Method.t (the public method type), used by the clients.
val meth_to_h1 :
meth ->
[> `CONNECT
| `DELETE
| `GET
| `HEAD
| `OPTIONS
| `Other of string
| `POST
| `PUT
| `TRACE ]val meth_of_string : string -> methval meth_to_string : meth -> stringRequest Builders
val create : meth -> Uri.t -> builderCreate a builder with the given method and URI
val get : Uri.t -> builderCreate a GET request builder
val post : Uri.t -> builderCreate a POST request builder
val put : Uri.t -> builderCreate a PUT request builder
val delete : Uri.t -> builderCreate a DELETE request builder
val patch : Uri.t -> builderCreate a PATCH request builder
val head : Uri.t -> builderCreate a HEAD request builder
val options : Uri.t -> builderCreate an OPTIONS request builder
val of_uri : meth -> Uri.t -> builderCreate a request builder from a Uri
Headers
val header : string -> string -> builder -> builderAdd a header to the request
val headers : (string * string) list -> builder -> builderAdd multiple headers to the request
val content_type : string -> builder -> builderSet the Content-Type header
val accept : string -> builder -> builderSet the Accept header
val user_agent : string -> builder -> builderSet the User-Agent header
val bearer : string -> builder -> builderSet Bearer authentication
val basic_auth : user:string -> pass:string -> builder -> builderSet Basic authentication
val cookie : string -> string -> builder -> builderSet a cookie header
val cookies : (string * string) list -> builder -> builderSet cookies from a list
Query Parameters
val query : string -> string -> builder -> builderAdd a query parameter
val queries : (string * string) list -> builder -> builderAdd multiple query parameters
Body
val body : body -> builder -> builderSet the request body
val body_string : ?content_type:string -> string -> builder -> builderSet the body as a string with optional content type
val body_json : string -> builder -> builderSet the body as a JSON string
val form : (string * string) list -> builder -> builderSet the body as form data
Building
val build : builder -> requestBuild the final request
val url : request -> stringGet the URL as a string
val host : request -> stringGet the host from the request
val port : request -> intGet the port from the request
val path : request -> stringGet the path from the request
val path_and_query : request -> stringGet the path and query from the request
val is_https : request -> boolCheck if the request is HTTPS
Body Encoding
val encode_form : (string * string) list -> stringEncode form data as URL-encoded string
val body_to_string : body -> stringGet the body as a string
val body_length : body -> intGet the Content-Length for the body