Phoenix/Elixir - cURL 有效,但 HTTPoison 失败
Phoenix/Elixir - cURL works, but HTTPoison fails
在我的 Phoenix 应用程序中,我正在尝试使用 HTTPoison HTTP 客户端 (https://hexdocs.pm/httpoison/api-reference.html) 向 AgileCRM API 发出 post 请求。我能够使用 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
哪个 returns 状态 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
哪个returns
%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
header,但也许不是?
HTTPPoison 还允许使用 options
参数,其中一个选项(哈!)是“:proxy_auth - 代理身份验证 {User, Password} 元组”,但传递 [proxy_auth: {"admin@test.com", "api_key"}]
产生相同的结果。
如有任何想法,我们将不胜感激
实际上 curl -u
做的更多,检查这个答案:What is -u flag on cURL actually doing?。
它将您的 user:password 编码为 base 64 字符串并添加 Basic
前缀。
你应该像这样发送header
Authorization: Basic base64 encoded string
curl -u
的等价物是 basic_auth
option of hackney
,而不是 HTTPoison 的 proxy_auth
。你可以这样使用它:
HTTPoison.post!(url, body, headers, hackney: [basic_auth: {"admin@test.com", "api_key"}])
在我的 Phoenix 应用程序中,我正在尝试使用 HTTPoison HTTP 客户端 (https://hexdocs.pm/httpoison/api-reference.html) 向 AgileCRM API 发出 post 请求。我能够使用 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
哪个 returns 状态 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
哪个returns
%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
header,但也许不是?
HTTPPoison 还允许使用 options
参数,其中一个选项(哈!)是“:proxy_auth - 代理身份验证 {User, Password} 元组”,但传递 [proxy_auth: {"admin@test.com", "api_key"}]
产生相同的结果。
如有任何想法,我们将不胜感激
实际上 curl -u
做的更多,检查这个答案:What is -u flag on cURL actually doing?。
它将您的 user:password 编码为 base 64 字符串并添加 Basic
前缀。
你应该像这样发送header
Authorization: Basic base64 encoded string
curl -u
的等价物是 basic_auth
option of hackney
,而不是 HTTPoison 的 proxy_auth
。你可以这样使用它:
HTTPoison.post!(url, body, headers, hackney: [basic_auth: {"admin@test.com", "api_key"}])