今天刚从Reason和OCaml开始。我从https://github.com/esy-ocaml/hello-reason样品我想做一个HTTP API调用,所以我安装了带有:esy add @opam/cohttp-lwt
的ocaml-cohttp。
现在,我想在hello reason入门示例中使用该库(或您可能有的任何建议(。
我找不到关于如何导入它的参考文档。我尝试过:
open cohttp-lwt
我可以在Reason代码文件中使用OCaml库吗?
是的,唯一的区别是语法。客户端教程可以直接翻译,并自动翻译为:
open Lwt;
open Cohttp;
open Cohttp_lwt_unix;
let body =
Client.get(Uri.of_string("https://www.reddit.com/"))
>>= (
((resp, body)) => {
let code = resp |> Response.status |> Code.code_of_status;
Printf.printf("Response code: %dn", code);
Printf.printf(
"Headers: %sn",
resp |> Response.headers |> Header.to_string,
);
body
|> Cohttp_lwt.Body.to_string
>|= (
body => {
Printf.printf("Body of length: %dn", String.length(body));
body;
}
);
}
);
let () = {
let body = Lwt_main.run(body);
print_endline("Received bodyn" ++ body);
};
编辑:hello-reason
使用ocaml沙丘,因此您还必须将cohttp-lwt-unix
添加到项目的dune
文件中的libraries
节中,如此处的客户端教程所示