【问题标题】:Post request not working with Rest-Client but It's OK with Faraday and Postman发布请求不能与 Rest-Client 一起使用,但 Faraday 和 Postman 可以
【发布时间】: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


【解决方案1】:

从 Rest_Client 调试中,您可能会发现 Data 是空的。

D, [2019-01-18T00:37:31.105052 #12472] DEBUG -- : [httplog] Data:

看来你对待它是错误的。试试这个:

curl -X POST \
  'https://mysite.vn/api/tokens' \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: 5dea5d74-80e6-41b1-87bc-ee3c81cf754a' \
  -H 'cache-control: no-cache' \
  -d '{"email": "my_email@gmail.com", "password": "my_password12", "grant_type":"password"}'

有关-d 标志的更多信息,请参阅man curl

【讨论】:

  • 您好,我的API可以通过两种方式请求+在URL中带参数。这就是为什么你看不到 CURL 中的数据 + 正文中有参数,CURL 会像你之前说的那样: curl -X POST \ mysite.vn/v2/tokens \ -H 'Content-Type: application/json' \ -H 'Postman -令牌:ee0fcbeb-3772-45bf-bdef-190201da825a'\ -H'缓存控制:无缓存'\ -d'{“电子邮件”:“my_account@gmail.com”,“密码”:“my_password12”, “grant_type”:“密码”}'
猜你喜欢
  • 1970-01-01
  • 2019-04-27
  • 1970-01-01
  • 1970-01-01
  • 2019-01-04
  • 1970-01-01
  • 2013-09-12
  • 2023-03-21
  • 2020-09-15
相关资源
最近更新 更多