Curl 菲尼克斯/长生不老药-卷曲有效,但HTTPoison失败

Curl 菲尼克斯/长生不老药-卷曲有效,但HTTPoison失败,curl,elixir,phoenix-framework,httpoison,agile-crm,Curl,Elixir,Phoenix Framework,Httpoison,Agile Crm,在我的Phoenix应用程序中,我尝试使用httpoisonHTTP客户端()向AgilecrmAPI发出post请求。我可以使用cURL成功地发出请求,但我在Phoenix中复制它的尝试失败了,出现了401未经授权的错误 我成功的卷发: $ curl https://domain.agilecrm.com/dev/api/contacts/search/email \ -H "Accept: application/json" \ -d 'email_ids=["contact@test.co

在我的Phoenix应用程序中,我尝试使用httpoisonHTTP客户端()向AgilecrmAPI发出post请求。我可以使用cURL成功地发出请求,但我在Phoenix中复制它的尝试失败了,出现了401
未经授权的
错误

我成功的卷发:

$ 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
和请求的数据

我失败的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}
%HTTPoison.Response{body:“\n\n401未经授权\n\n\n错误:未经授权\n\n”,
标题:[{“X-TraceUrl”,“/appstats/details?time=1509129565372&type=json”},
{“WWW-Authenticate”,“基本领域=\“agilecrm\”},
{“内容类型”,“text/html;charset=utf-8”},
{“X-Cloud-Trace-Context”,“8de994n2tbu2o356891bc3e6”},
{“日期”,“2017年10月27日星期五18:39:25 GMT”},{“服务器”,“谷歌前端”},
{“内容长度”,“200”}],
请求url:“https://domain.agilecrm.com/dev/api/contacts/search/email“,状态代码:401}
从消息中,我假设(可能是错误的)问题在于授权数据。我的理解是,cURL中的
-u
标志等同于添加
授权
头,但可能不是

HTTPoison还允许一个
选项
参数,其中一个选项(ha!)是:“proxy_auth-proxy authentication{User,Password}元组”,但传递
[proxy_auth:{”admin@test.com“,”api_key“}]
产生相同的结果


如果您对此有任何想法,我们将不胜感激。

实际上
curl-u
做得更多,请检查以下答案:

它将user:password编码为base 64字符串,并添加
Basic
前缀

您应该发送这样的标题

Authorization: Basic base64 encoded string

curl-u
的等价物是,而不是HTTPoison的
proxy\u auth
。您可以这样使用它:

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

完美的多伯特,你又救了我。谢谢-公认的答案对我有用,但知道这一点真的很有用(我不知道)。