【发布时间】:2019-01-17 17:43:02
【问题描述】:
我有一个登录测试站点的 POST 请求 此请求适用于 cURL 或 Faraday gem,但不适用于 rest-client gem。
1) 使用 cURL
curl -X POST \
'https://mysite.vn/api/tokens?email=my_email@gmail.com&password=my_password12&grant_type=password' \
-H 'Content-Type: application/json' \
-H 'Postman-Token: 5dea5d74-80e6-41b1-87bc-ee3c81cf754a' \
-H 'cache-control: no-cache'
#
2) 与法拉第
request_body = '{"email": "my_email@gmail.com", "password": "my_password12", "grant_type":"password"}'
conn = Faraday.new(:url => 'https://mysite.vn/api/tokens')
response_faraday = conn.post do |req|
req.headers['Content-Type'] = 'application/json'
req.body = request_body
end
puts response_faraday.status
#
3) 使用休息客户端
new_payload = {
email: 'my_email@gmail.com',
password: 'my_password12',
grant_type: 'password'
}
response_resclient = RestClient::Request.execute(
method: :post,
url: 'https://mysite.vn/api/tokens',
payload: new_payload.to_json,
headers: {'Content-Type': 'application/json', 'Accept': 'application/json'})
放入 response_resclient.code
对于 1 和 2,两者都是正确的。他们都返回状态 201,登录成功,但它失败了 3 - Rest_Client。状态为 404
我尝试使用 httplog 进行检查,这里是日志文件
法拉第: D,[2019-01-18T00:25:10.228043 #7764] 调试 -- : [httplog] 正在连接:mysite.vn:443
D, [2019-01-18T00:25:11.355649 #7764] DEBUG -- : [httplog] Sending: POST http://mysite.vn:443/api/v2/tokens/
D, [2019-01-18T00:25:11.356434 #7764] DEBUG -- : [httplog] Data: {"email": "my_email@gmail.com", "password": "my_password12", "grant_type":"password"}
D, [2019-01-18T00:25:11.362419 #7764] DEBUG -- : [httplog] Status: 201
D, [2019-01-18T00:25:11.362419 #7764] DEBUG -- : [httplog] Benchmark: 0.053334 seconds
D, [2019-01-18T00:25:11.363419 #7764] DEBUG -- : [httplog] Response:
201
Rest_Client:
D, [2019-01-18T00:37:29.982352 #12472] DEBUG -- : [httplog] Connecting: mysite.vn:443
D, [2019-01-18T00:37:31.105052 #12472] DEBUG -- : [httplog] Sending: POST http://mysite.vn:443/api/v2/tokens/
D, [2019-01-18T00:37:31.105052 #12472] DEBUG -- : [httplog] Data:
D, [2019-01-18T00:37:31.111003 #12472] DEBUG -- : [httplog] Status: 404
D, [2019-01-18T00:37:31.112000 #12472] DEBUG -- : [httplog] Benchmark: 0.010938 seconds
D, [2019-01-18T00:37:31.112997 #12472] DEBUG -- : [httplog] Response:
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
使用 Rest-client gem,在将 post 请求发送到 mysite.vn 后返回 404 错误,代码为 443。我对这种情况没有理想。 请你帮我解释一下这个案子。以及如何使用 Rest-Client 纠正此问题
【问题讨论】:
-
希望你有一个
require 'json'吗? -
是的,我需要'json',但它没有解决T_T
标签: ruby api rest-client faraday