使用Ocamlnet 3的正确方法 - Http_client.Convenience.http_post [英] The correct way to use Ocamlnet 3 - Http_client.Convenience.http_post

查看:197
本文介绍了使用Ocamlnet 3的正确方法 - Http_client.Convenience.http_post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Http_client.Convenience.http_post 来制作 http post请求






API非常简单:


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



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







我想做什么是建立一个 http post 请求,通过 google flight 获取航班信息,解释为第1部分在这里:。



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


I am trying to use Http_client.Convenience.http_post to make a http post request.


The API is fairly simple:

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.


What I wish to do is to construct a http post request to get the flight information via google flights, explained as part 1 in here: http://www.nohup.in/blog/using-json-google-flights

To maintain the format of the Post request, I took a screenshot as this:


So finally, I construct a Http_client.Convenience.http_post for it:

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());;


When I run it, it just give me the following 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>


Can anyone tell me why? What's wrong with my http_post?

解决方案

Drop this from post_para, it is not an HTTP Header, OCamlnet will send that automatically for you: "(Request-Line)", "POST /flights/rpc HTTP/1.1".

To send the headers and POST data separately you need to set the headers use set_request_header on the http_call object.

Also the Convenience module in OCamlnet will send the data as application/x-www-form-urlencoded, but I think you need the data sent as is. You can do that by using Http_client.post_raw.

这篇关于使用Ocamlnet 3的正确方法 - Http_client.Convenience.http_post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆