gpt4 book ai didi

http-post - 使用 Ocamlnet 3 的正确方法 - Http_client.Convenience.http_post

转载 作者:行者123 更新时间:2023-12-04 04:53:50 26 4
gpt4 key购买 nike

我正在尝试使用 Http_client.Convenience.http_post制作 http post request .

API 相当简单:

val http_post : string -> (string * string) list -> string

Does a "POST" request with the given URL and returns the response body. The list contains the parameters send with the POST request.



我想做的是构建一个 http post通过 google flights 请求获取航类信息,解释为 part 1在这里: http://www.nohup.in/blog/using-json-google-flights

保持 Post request 的格式,我截图如下:

enter image description here

所以最后,我构造了一个 Http_client.Convenience.http_post为了它:
open Http_client.Convenience;;

let post_para = [("(Request-Line)", "POST /flights/rpc HTTP/1.1");
("Host", "www.google.com");
("Content-Type", "application/json; charset=utf-8");
("X-GWT-Permutation", "0BB89375061712D90759336B50687E78");
("X-GWT-Module-Base", "http://www.google.com/flights/static/");
("Referer", "http://www.google.com/flights/");
("Content-Length", "275");
("Cookie", "PREF=ID=2dc218fc830df28d:U=29aaf343dd519bca:FF=0:TM=1307225398:LM=1308065727:GM=1:S=RWC3dYzVvVSpkrlz; NID=52=VTp1QILW1ntPlrkLx7yLUtOYhchNk35G4Lk35KBd7A3lCznVV5glz7lwDoDP2RkjtTJVNZSomv3iffPqiJz4oXfpoph3ljb2eInGOe-FwosvrmSXPpnLkEWxMHIbuaid; S=travel-flights=YFCjkd9M9h3Z_uEqBmgynA");
("Pragma", "no-cache");
("Cache-Control", "no-cache");
("data", "[,[[,\"fs\",\"[,[,[\"SJC\"]\n,\"2012-04-05\",[\"EWR\",\"JFK\",\"LGA\"]\n,\"2012-04-12\"]\n]\n\"]],[,[[,\"b_ca\",\"54\"],[,\"f_ut\",\"search;f=SJC;t=EWR,JFK,LGA;d=2012-04-05;r=2012-04-12\"],[,\"b_lr\",\"11:36\"],[,\"b_lr\",\"1:1528\"],[,\"b_lr\",\"2:1827\"],[,\"b_qu\",\"3\"],[,\"b_qc\",\"1\"]]]]")];;

let search () = try (http_post "http://www.google.com/flights/rpc" post_para) with
Http_client.Http_error (id, msg) -> msg;;

let _ = print_endline (search());;

当我运行它时,它只会给我以下 error html page :
<HTML>
<HEAD>
<TITLE>Internal Server Error</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Internal Server Error</H1>
<H2>Error 500</H2>
</BODY>
</HTML>

谁能告诉我为什么?我的http_post怎么了?

最佳答案

从 post_para 中删除它,它不是 HTTP header ,OCamlnet 会自动为您发送:"(Request-Line)", "POST /flights/rpc HTTP/1.1" .

要分别发送 header 和 POST 数据,您需要使用 set_request_header 设置 header 在 http_call 对象上。

此外,OCamlnet 中的便利模块会将数据作为 application/x-www-form-urlencoded 发送,但我认为您需要按原样发送数据。您可以使用 Http_client.post_raw 来做到这一点。 .

关于http-post - 使用 Ocamlnet 3 的正确方法 - Http_client.Convenience.http_post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17047223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com