How to send http(s) request in Eio?

I haven’t found a single example of it. How to? Preferably, 10 parallel ones.

And does Eio on its own support http(s) requests at all? Or will an external library have to be used?

I think cohttp-eio is an option. It has a cohttp-eio 6.0.0 (latest) · OCaml Package or some more specialised functions like get, post, etc.

Yes; there’s an example showing how to use it here:

Is there an easier example for using https scheme with cohttp-eio other than what is shown in ocaml-cohttp/cohttp-eio/examples/client_tls.ml at master · mirage/ocaml-cohttp · GitHub ? Seems like a lot of setup just to use https with the client.

If you are interested by another scheduler, we released httpcats (opam install httpcats) which can do http/1.1 and h2 requests and use Miou as a scheduler - you can see a simple example into the README.md. Miou already sets a pool of domain and a simple use of Miou.call allows you to parallelize multiple requests (you can see some details about parallelism & Miou here).

1 Like

Piaf takes care of HTTPS setup, try Piaf.Client.Oneshot.get

Something like:

let just_google_it ~env ~sw query =
  match Piaf.Client.Oneshot.get
    ~sw
    env
    (Uri.of_string ("https://www.google.com/search?q=" ^ query)
  with
  | Error _ -> failwith query
  | Ok { Piaf.Response.status = `OK; body } ->
  match Piaf.Body.to_string body with
  | Ok s -> s
  | Error _ -> failwith query
    
2 Likes

Would you be able to add an example of how one can serve a local directory using httpcats’ server functionality?

I think it’s doable. You actually have an example of a simple server (used for benchmarks) here. You just need to extend it with Sys.readdir and open_in :slight_smile: .

1 Like

I’ve removed _build. Then

% dune build   
File "_none_", line 1:             
Error: No implementations provided for the following modules:
         Logs_threaded referenced from bin/.main.eobjs/native/dune__exe__Main.cmx

What is this about?

dune

(executable
    (public_name p2)
    (name main)
    (preprocess (pps lwt_ppx))
    (libraries
        p2
        eio
        lwt
        cohttp-eio
        cohttp-lwt-unix
        eio_main
        yojson))

some of the dependencies have been left from the previous examples that I’ve tried