ruby - 使用用户名和密码休息客户端帖子
问题描述
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'}
也没有工作。我已检查用户名和密码是否正确。
它显示标题中缺少用户名。
提前致谢。
解决方案
根据描述,似乎在使用 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'}
注意:我刚刚更新了在标头哈希中传递的标头键的表示。
推荐阅读
- java - 如何在自定义注释中进行 Rest API 调用?
- php - Wordpress REST API - 如何创建具有空命名空间的 API 路由?
- python - 如何根据列计算单词?
- android - 获取 Firestore 集合中所有键的名称
- python - 如何处理烧瓶 URL 应用程序中的特殊字符?
- elasticsearch - Elasticsearch 中的多匹配查询和评分计算
- python - 解析字符串列表并根据这些字符串的切片返回连接字符串的最佳方法是什么?
- node.js - 无轮询的无尽滚动(实时)
- python - 如何使用 Selenium WebDriver Python 读取在浏览器中打开的 PDF 文件?
- reactjs - React 如何在 useEffect 之外清理 setTimeout