首页 > 解决方案 > 使用用户名和密码休息客户端帖子

问题描述

curl --request POST \
  --url https://example.com/token/grant \
  --header 'password: password' \
  --header 'username: user' \
  --data '{"app_key":"my_app_key","app_secret":"my_app_secret"}'

我正在尝试使用 rest 客户端为上述 curl 请求获取等效的 ruby​​ 代码。

我努力了:

auth = 'Basic ' + Base64.encode64( "#{AA['user_name']}:#{AA['password']}" ).chomp
response = RestClient.post'https://example.com/token/grant', {"app_key":"my_app_key","app_secret":"my_app_secret"}, {  'Authorization' => auth , :accept => :json, 'Content-Type': 'application/json' }
response = JSON.parse(response)

那没起效。

第二次尝试:

RestClient.post 'https://example.com/token/grant', {"app_key":"my_app_key","app_secret":"my_app_secret"}.to_json,  {'username': 'username', 'password': 'password', content_type: 'application/json'}

也没有工作。我已检查用户名和密码是否正确。

它显示标题中缺少用户名。

提前致谢。

标签: rubycurlruby-on-rails-5rest-client

解决方案


根据描述,似乎在使用 RestClient 运行请求时,标头操作无法处理标头(用户名,因为它是标头哈希中的第一个键)。

我已经在自定义代码上执行了以下命令并且它有效。

 RestClient.post 'https://example.com/token/grant', {"app_key":"my_app_key","app_secret":"my_app_secret"}.to_json,  {username: 'username', password: 'password', content_type: 'application/json'}

注意:我刚刚更新了在标头哈希中传递的标头键的表示。


推荐阅读