http post request erlang

眉间皱痕 提交于 2019-12-06 03:28:43

问题


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

For the POST request I use this:

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

While for the HEAD/GET I use:

  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. I tried with empty lists but I got:

  ** exception error: no function clause matching

Thank you very much


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/6173493/http-post-request-erlang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!