这是使用库Cohttp的一个简单示例:
open Lwt
open Cohttp
open Cohttp_lwt_unix
let body =
Client.get (Uri.of_string "http://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)
Run Code Online (Sandbox Code Playgroud)
我正在尝试编译它
ocaml my_test1.ml
Run Code Online (Sandbox Code Playgroud)
错误:
错误:未绑定模块Lwt
如何在我的应用程序中实际包含/要求模块Lwt?
更新
也:
$ ocamlbuild
bash: …Run Code Online (Sandbox Code Playgroud) ocaml ×1