【问题标题】:http post request erlanghttp post请求erlang
【发布时间】:2015-09-05 18:33:38
【问题描述】:

我有几个函数可以执行 HTTP POST/GET/HEAD 请求。

对于 POST 请求,我使用这个:

  http:request(post, {Url, [], ContentType, Body}, [], []).

而对于我使用的 HEAD/GET:

  http:request(Method, {Url, []}, [], [])

如何将这两个调用写成一个唯一的调用? POST 请求具有与 GET/HEAD 请求相关的这两个附加变量。我尝试使用空列表,但得到了:

  ** exception error: no function clause matching

非常感谢

【问题讨论】:

    标签: http erlang


    【解决方案1】:

    要只调用一次httpc,您需要从调用中提取Request 元组,因为这是您使用它们时方法之间的独特之处:

    post(URL, ContentType, Body) -> request(post, {URL, [], ContentType, Body}).
    get(URL)                     -> request(get,  {URL, []}).
    head(URL)                    -> request(head, {URL, []}).
    
    request(Method, Request) ->
        httpc:request(Method, Request, [], []).
    

    【讨论】:

      【解决方案2】:
      Body = "name=<<name>>&pass=<<pass>>",
      httpc:request(post, {Url, [], "application/x-www-form-urlencoded", Body}, [], []).
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-30
        • 2012-07-10
        • 2015-05-13
        • 2015-06-01
        • 2017-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多