http post请求erlang

http post请求erlang

问题描述:

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

I have a couple of functions that perform HTTP POST/GET/HEAD requests.

对于POST请求,我使用:

For the POST request I use this:

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

对于HEAD / GET,我使用:

While for the HEAD/GET I use:

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

如何将这两个电话写入独一无二的电话? POST请求具有关于GET / HEAD请求的两个附加变量。我尝试用空列表,但我得到:

How can I write this two calls in a unique one? POST request has those two additional variables with respect to GET/HEAD request. I tried with empty lists but I got:

  ** exception error: no function clause matching

非常感谢

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

To use the call to httpc only once, you need to extract the Request tuple from the call because that's what's unique between the methods as you use them:

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, [], []).