Phoenix/Elixir-cURL有效,但是HTTPoison失败 [英] Phoenix/Elixir - cURL works, but HTTPoison fails

查看:137
本文介绍了Phoenix/Elixir-cURL有效,但是HTTPoison失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Phoenix应用程序中,我正在尝试使用HTTPoison HTTP客户端( https://hexdocs.pm/httpoison/api-reference.html )向AgileCRM API发出发布请求.我能够使用cURL发出成功的请求,但是我尝试在Phoenix中复制它的尝试失败,并显示401 UNAUTHORIZED错误.

In my Phoenix app, I am trying to use the HTTPoison HTTP client (https://hexdocs.pm/httpoison/api-reference.html) to make a post request to the AgileCRM API. I was able to use cURL to make a successful request, but my attempt to replicate it in Phoenix is failing with a 401 UNAUTHORIZED error.

我成功的cURL:

$ curl https://domain.agilecrm.com/dev/api/contacts/search/email \
-H "Accept: application/json" \
-d 'email_ids=["contact@test.com"]' \
-u admin@test.com:api_key

返回状态200和请求的数据.

which returns status 200 and the requested data.

我失败的HTTPoison:

My failing HTTPoison:

url = "https://domain.agilecrm.com/dev/api/contacts/search/email"
body = Poison.encode!(%{"body": "email_ids=['contact@test.com']"})
headers = [{"Accept", "application/json"}, {"Authorization", "admin@test.com:api_key"}]

response = HTTPoison.post!(url, body, headers)

IO.inspect response

返回

%HTTPoison.Response{body: "<html><head>\n<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<title>401 UNAUTHORIZED</title>\n</head>\n<body text=#000000 bgcolor=#ffffff>\n<h1>Error: UNAUTHORIZED</h1>\n</body></html>\n",
headers: [{"X-TraceUrl", "/appstats/details?time=1509129565372&type=json"},
{"WWW-Authenticate", "Basic realm=\"agilecrm\""},
{"Content-Type", "text/html; charset=utf-8"},
{"X-Cloud-Trace-Context", "8de994n2tbu2o356891bc3e6"},
{"Date", "Fri, 27 Oct 2017 18:39:25 GMT"}, {"Server", "Google Frontend"},
{"Content-Length", "200"}],
request_url: "https://domain.agilecrm.com/dev/api/contacts/search/email", status_code: 401}

根据消息,我假设(可能是错误地)问题出在授权数据上.我的理解是,cURL中的-u标志等同于添加Authorization标头,但也许不是吗?

From the message, I'm assuming (perhaps incorrectly) that the issue is with the authorization data. My understanding was that the -u flag in a cURL is the equivalent of adding an Authorization header, but perhaps not?

HTTPoison还允许使用options参数,其中一个选项(ha!)是:proxy_auth-代理身份验证{User,Password}元组",但传递[proxy_auth: {"admin@test.com", "api_key"}]会产生相同的结果.

HTTPoison also allows for an options parameter, and one of the options (ha!) is ":proxy_auth - proxy authentication {User, Password} tuple", but passing [proxy_auth: {"admin@test.com", "api_key"}] produces the same result.

任何对此的想法将不胜感激

Any thoughts on this would be much appreciated

推荐答案

curl -u等效的是

The equivalent of curl -u is the basic_auth option of hackney, not HTTPoison's proxy_auth. You can use it like this:

HTTPoison.post!(url, body, headers, hackney: [basic_auth: {"admin@test.com", "api_key"}])

这篇关于Phoenix/Elixir-cURL有效,但是HTTPoison失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆