Skip to content

Instantly share code, notes, and snippets.

@rbjorklin
Last active September 18, 2023 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbjorklin/4c32e749881fece0d62b28ecf339283b to your computer and use it in GitHub Desktop.
Save rbjorklin/4c32e749881fece0d62b28ecf339283b to your computer and use it in GitHub Desktop.
eio https request
(**
#require "tls-eio"
#require "cohttp-eio";;
#require "eio_main";;
#require "mirage-crypto-rng-eio";;
*)
open Eio
open Cohttp_eio
let tls_config =
let null ?ip:_ ~host:_ _certs = Ok None in
Tls.Config.client ~authenticator:null ()
let get ?headers ~net (host, resource) =
Net.with_tcp_connect ~service:"https" ~host net @@ fun conn ->
let conn =
Tls_eio.client_of_flow tls_config
?host:
(Domain_name.of_string_exn host |> Domain_name.host |> Result.to_option)
conn
in
let resp =
Client.get ?headers ~conn ~host:("https://" ^ host)
(object
method net = net
end)
resource
in
Client.read_fixed resp |> print_endline
let () =
Eio_main.run @@ fun env ->
Mirage_crypto_rng_eio.run (module Mirage_crypto_rng.Fortuna) env @@ fun () ->
let net = Eio.Stdenv.net env in
let headers = Http.Header.init_with "Host" "www.example.org" in
get ~headers ~net ("www.example.org", "/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment