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

时间:2013-06-11 14:54:51

标签: http-post ocaml

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


API非常简单:

  

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

     

请求具有给定URL的“POST”请求并返回响应   身体。该列表包含使用POST请求发送的参数。


我希望通过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

出了什么问题

1 个答案:

答案 0 :(得分:3)

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

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

OCamlnet中的Convenience模块也会将数据作为application / x-www-form-urlencoded发送,但我认为您需要按原样发送数据。您可以使用Http_client.post_raw

来完成此操作