2

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.

glennsl
  • 28,186
  • 12
  • 57
  • 75
illabout
  • 3,517
  • 1
  • 18
  • 39
  • 1
    Have you tried also installing the `ssl` package from OPAM? – antron Aug 19 '17 at 18:51
  • @antron I believe I already have the `ssl` package installed. I updated the question showing this. – illabout Aug 20 '17 at 02:15
  • 2
    Interesting. A lot of these packages have been reorganized recently. `cohttp-lwt-unix` depends on `conduit-lwt-unix`, which depends (optionally) on `ssl` and `lwt`. I suggest trying to install `lwt_ssl`, then maybe `opam reinstall conduit-lwt-unix` to get it to reconfigure itself. If that doesn't work, PUNISHED BAYGE from Discord suggests pinning the packages to their development versions, e.g. `opam pin add conduit-lwt-unix --dev-repo`, etc. – antron Aug 20 '17 at 07:38
  • 2
    FYI I was just able to get Cohttp with SSL working this way, without resorting to pins. – antron Aug 20 '17 at 07:46
  • 2
    Also, this is a bug in the packaging of `conduit-lwt-unix`, so no, there is no documentation to find for this. I opened an [issue](https://github.com/mirage/ocaml-conduit/issues/237) on it. – antron Aug 20 '17 at 07:59
  • @antron Installing `lwt_ssl` and then reinstalling `conduit-lwt-unix` fixed my problem. Thanks! Also, thanks for creating the issue on ocaml-conduit. – illabout Aug 20 '17 at 09:52
  • This also worked for me. Thank's @antron! – Jagare Aug 22 '17 at 18:41

0 Answers0