Hi, Im struggling to work out the method to add authentication to a Client.get in the Cohttp_lwt_unix package. Got this far, but cant work out what’s wrong:
open Lwt
open Cohttp
open Cohttp_lwt_unix
let body =
let headers = Header.init ()
|> fun h -> Header.add h "content-type" "application/json"
|> fun h -> Header.add_authorization h ("username", "password") in
Client.get ~headers (Uri.of_string "https://www.reddit.com/") >>= fun (resp, body) ->
let code = resp |> Response.status |> Code.code_of_status in
Printf.printf "Response code: %d\n" code;
Printf.printf "Headers: %s\n" (resp |> Response.headers |> Header.to_string);
body |> Cohttp_lwt.Body.to_string >|= fun body ->
Printf.printf "Body of length: %d\n" (String.length body);
body
let () =
let body = Lwt_main.run body in
print_endline ("Received body\n" ^ body)