···
let open Cohttp_lwt_unix in
let headers = Header.add_list (Header.init ()) headers in
+
(* Debug: print request details *)
+
Printf.printf "\n===== HTTP REQUEST =====\n";
+
Printf.printf "URI: %s\n" (Uri.to_string uri);
+
Printf.printf "METHOD: POST\n";
+
Printf.printf "HEADERS:\n";
+
Header.iter (fun k v -> Printf.printf " %s: %s\n" k v) headers;
+
Printf.printf "BODY:\n%s\n" body;
+
Printf.printf "======================\n\n";
let* resp, body = Client.post ~headers ~body:(Cohttp_lwt.Body.of_string body) uri in
let* body_str = Cohttp_lwt.Body.to_string body in
let status = Response.status resp |> Code.code_of_status in
+
(* Debug: print response details *)
+
Printf.printf "\n===== HTTP RESPONSE =====\n";
+
Printf.printf "STATUS: %d\n" status;
+
Printf.printf "HEADERS:\n";
+
Response.headers resp |> Header.iter (fun k v -> Printf.printf " %s: %s\n" k v);
+
Printf.printf "BODY:\n%s\n" body_str;
+
Printf.printf "========================\n\n";
if status >= 200 && status < 300 then
Lwt.return (Error (HTTP_error (status, body_str))))
+
let error_msg = Printexc.to_string e in
+
Printf.printf "\n===== HTTP ERROR =====\n%s\n======================\n\n" error_msg;
+
Lwt.return (Error (Connection_error error_msg)))
(** Make a raw JMAP API request
···
(* API token (bearer authentication) *)
"Bearer " ^ config.authentication_token
+
Printf.printf "Using authorization header: %s\n" auth_header;
("Content-Type", "application/json");
("Content-Length", string_of_int (String.length body));
···
let get_session uri ?username ?authentication_token ?api_token () =
match (username, authentication_token, api_token) with
+
| (Some u, Some t, _) ->
+
let auth = "Basic " ^ Base64.encode_string (u ^ ":" ^ t) in
+
Printf.printf "Session using Basic auth: %s\n" auth;
+
("Content-Type", "application/json");
+
("Authorization", auth)
+
| (_, _, Some token) ->
+
let auth = "Bearer " ^ token in
+
Printf.printf "Session using Bearer auth: %s\n" auth;
+
("Content-Type", "application/json");
+
("Authorization", auth)
| _ -> [("Content-Type", "application/json")]