在Erlang中设置HTTPC post请求中的标头

时间:2015-02-03 06:18:36

标签: post erlang ejabberd

我正在尝试从Erlang发出HTTPC帖子请求:

httpc:request(post, {"https://android.googleapis.com/gcm/send",[{"Authorization","key=api_key_generated_at_googleaccount"}],[{"Content-Type","application/json"}],Body},[],[])

但每次我发出此请求时,我的Erlang shell都会挂起。请求中有任何问题吗?

1 个答案:

答案 0 :(得分:2)

我认为您在httpc:http://erlang.org/doc/man/httpc.html的手册中稍微混淆了http:request/4的论点

request(Method, Request, HTTPOptions, Options)

   Request          = request()
   request()        = {url(), headers()} |
                      {url(), headers(), content_type(), body()}
   content_type()   = string()

上面你可以看到content_type()应该是一个字符串()而不是[{string(),string()}]。

如果将整个request()放入变量中,则更容易阅读:

10> inets:start().
ok
11> ssl:start().
ok
12> Body = "some cool json",
12> Request = {"https://android.googleapis.com/gcm/send", [{"Authorization","key=blabla"}], "application/json", Body},
12> httpc:request(post, Request, [], []).
{ok,{{"HTTP/1.1",401,"Unauthorized"},
     [{"cache-control","private, max-age=0"},
      {"date","Tue, 03 Feb 2015 07:19:07 GMT"},
      {"accept-ranges","none"},
      {"server","GSE"},
      {"vary","Accept-Encoding"},
      {"content-length","147"},
      {"content-type","text/html; charset=UTF-8"},
      {"expires","Tue, 03 Feb 2015 07:19:07 GMT"},
      {"x-content-type-options","nosniff"},
      {"x-frame-options","SAMEORIGIN"},
      {"x-xss-protection","1; mode=block"},
      {"alternate-protocol","443:quic,p=0.02"}],
     "<HTML>\n<HEAD>\n<TITLE>Unauthorized</TITLE>\n</HEAD>\n<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n<H1>Unauthorized</H1>\n<H2>Error 401</H2>\n</BODY>\n</HTML>\n"}}