我正在尝试使用 Http_client.Convenience.http_post
发出 http post 请求
。
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
获取航类信息,如第 1 部分
中所述这里:http://www.nohup.in/blog/using-json-google-flights
为了保持Post请求
的格式,我截图如下:
最后,我为其构造了一个 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());;
当我运行它时,它只会给我以下错误html页面
:
<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/