【问题标题】:Setting headers in a HTTPC post request in Erlang在 Erlang 的 HTTPC 发布请求中设置标头
【发布时间】:2015-04-02 06:35:45
【问题描述】:

我正在尝试从 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 都会挂起。请求有什么问题吗?

【问题讨论】:

    标签: post erlang ejabberd


    【解决方案1】:

    我认为您在httpc 的手册中混淆了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(),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"}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 2015-03-11
      • 2013-05-25
      • 2020-12-22
      • 2023-04-03
      相关资源
      最近更新 更多