使用OCaml cohttp Client.post方法(Using OCaml cohttp Client.post method)

编程入门 行业动态 更新时间:2024-10-09 23:14:57
使用OCaml cohttp Client.post方法(Using OCaml cohttp Client.post method)

我正在尝试使用OCaml cohttp包使用Client.post方法发送POST请求。 我查看了ocaml-cohttp / lib_test / test_net_lwt_client_and_server.ml中的示例以使用该方法,这是使用该函数的示例中的代码片段。

Client.post ~body:(Cohttp_lwt_body.of_string "barfoo") url

我在我自己的代码中使用的功能完全相同:

Client.post ~body:(Cohttp_lwt_body.of_string bodyString) (Uri.of_string stringURI) >>= function | Some (_, body) -> Cohttp_lwt_body.string_of_body body | None -> return ""

但我收到错误消息:

Error: This pattern matches values of type 'a option but a pattern was expected which matches values of type Cohttp.Response.t * Cohttp_lwt_body.t

我查看了https://github.com/mirage/ocaml-cohttp/issues/64 ,建议将~body标签更改为?body但后来又出现了不同的错误:

Error: This expression has type Cohttp_lwt_body.t but an expression was expected of type Cohttp_lwt_body.t option

有人可以解释一下如何正确使用这个功能吗?

I'm trying to use the OCaml cohttp package to send a POST request using the Client.post method. I looked at the example in ocaml-cohttp/lib_test/test_net_lwt_client_and_server.ml to use the method, here's a code snippet from the example which uses the function.

Client.post ~body:(Cohttp_lwt_body.of_string "barfoo") url

I'm using the function exactly the same way in my own code:

Client.post ~body:(Cohttp_lwt_body.of_string bodyString) (Uri.of_string stringURI) >>= function | Some (_, body) -> Cohttp_lwt_body.string_of_body body | None -> return ""

But I get the error message:

Error: This pattern matches values of type 'a option but a pattern was expected which matches values of type Cohttp.Response.t * Cohttp_lwt_body.t

I looked at https://github.com/mirage/ocaml-cohttp/issues/64 which suggested changing the ~body label to ?body but then I got a different error:

Error: This expression has type Cohttp_lwt_body.t but an expression was expected of type Cohttp_lwt_body.t option

Could someone please explain how to use this function correctly?

最满意答案

错误消息表明这是一个输入问题:

Error: This pattern matches values of type 'a option but a pattern was expected which matches values of type Cohttp.Response.t * Cohttp_lwt_body.t

应该重写您在bind( >>= )右侧的function体来处理Client.post返回的元组而不是option类型。 例如:

Client.post ~body:(Cohttp_lwt_body.of_string bodyString) (Uri.of_string stringURI) >>= fun (response, body) -> match response with | { Cohttp.Response.status = `OK; _ } -> ok_response_action body | { Cohttp.Response.status; _ } -> other_response_action status body

遗憾的是,cohttp目前没有易于访问的文档。 您需要直接从源引用.mli文件。 例如,有关Cohttp.Response.t类型结构的信息,请参见此处 。

The error message indicates that this is a typing issue:

Error: This pattern matches values of type 'a option but a pattern was expected which matches values of type Cohttp.Response.t * Cohttp_lwt_body.t

Your function body to the right of the bind (>>=) should be rewritten to handle the tuple returned by Client.post rather than an option type. For example:

Client.post ~body:(Cohttp_lwt_body.of_string bodyString) (Uri.of_string stringURI) >>= fun (response, body) -> match response with | { Cohttp.Response.status = `OK; _ } -> ok_response_action body | { Cohttp.Response.status; _ } -> other_response_action status body

cohttp does not, unfortunately, currently have easily accessible documentation. You would need to reference the .mli files from the source directly. For example, see here for information on the Cohttp.Response.t type's structure.

更多推荐

本文发布于:2023-07-27 02:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   cohttp   OCaml   Client   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!