我正在尝试使用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.tI 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 optionCould 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.tYour 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 bodycohttp 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.
更多推荐
发布评论