Phoenix / Elixir-cURL有效,但是HTTPoison失败

skw*_*eth 0 curl elixir phoenix-framework httpoison agile-crm

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

我成功的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
Run Code Online (Sandbox Code Playgroud)

返回状态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
Run Code Online (Sandbox Code Playgroud)

哪个返回

%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}
Run Code Online (Sandbox Code Playgroud)

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

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

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

Dog*_*ert 5

等效于curl -ubasic_auth选项hackney,而不是HTTPoison的选项proxy_auth。您可以像这样使用它:

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