简体   繁体   English

http 发布请求 erlang

[英]http post request erlang

I have a couple of functions that perform HTTP POST/GET/HEAD requests.我有几个函数可以执行 HTTP POST/GET/HEAD 请求。

For the POST request I use this:对于 POST 请求,我使用这个:

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

While for the HEAD/GET I use:而对于 HEAD/GET 我使用:

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

How can I write this two calls in a unique one?我怎样才能将这两个调用写成一个独特的调用? POST request has those two additional variables with respect to GET/HEAD request. POST 请求具有与 GET/HEAD 请求相关的这两个附加变量。 I tried with empty lists but I got:我尝试使用空列表,但我得到了:

  ** exception error: no function clause matching

Thank you very much非常感谢

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:要仅使用一次对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, [], []).
Body = "name=<<name>>&pass=<<pass>>",
httpc:request(post, {Url, [], "application/x-www-form-urlencoded", Body}, [], []).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM