How can I use the Cohttp library to GET
a URL over HTTPS?
Here is the code I'm trying to use:
open Lwt
open Cohttp
open Cohttp_lwt_unix
let url = "https://google.com"
let do_request =
Client.get (Uri.of_string url) >>= fun (resp, body) ->
body |> Cohttp_lwt.Body.to_string
let run () =
let body = Lwt_main.run do_request in
print_endline ("Received body\n" ^ body)
When I run this, I get the following error:
Fatal error: exception (Failure "Ssl not available")
How can I enable SSL when using Cohttp?
Also, as a bonus question, how should I have been able to figure this out without resorting to asking here? Is there some sort of Cohttp documentation I have missed?
@antron asked the question below, "Do you have the ssl
library installed?"
I do have the ssl
library installed:
$ opam list ssl
# Available packages for 4.05.0:
ssl 0.5.3 Bindings for OpenSSL
However, I'm not sure if this is relevant, but my cohttp
package doesn't show a depends
on ssl
:
$ opam info --field depends cohttp
base-bytes & jbuilder >= 1.0+beta10 & re & uri >= 1.9.0 & fieldslib & sexplib & ppx_fields_conv >= v0.9.0 & ppx_sexp_conv >= v0.9.0 & stringext & base64 >= 2.0.0 & magic-mime & fmt & logs & jsonm
I'm running on Arch Linux with OCaml version 4.05.0, installed through opam switch
.
Just in case, here is the jbuild
file I'm using:
(jbuild_version 1)
(executables
((names (main))
(libraries (cohttp cohttp-lwt-unix core lwt))))
(install
((section bin)
(files ((main.exe as my_example_prog)))))
I am running jbuilder
like the following. I get no errors when compiling:
$ jbuilder build @install
I am running the my_example_prog
executable like the following:
$ _build/install/default/bin/my_example_prog
Fatal error: exception (Failure "Ssl not available")
You can find the full OCaml project I'm using here.