rubygems - 使用带有 API 密钥的 HTTParty 请求
问题描述
我正在尝试对此API发出 GET 请求,该请求在 POSTMAN 中运行良好。在我的应用程序中,我使用 HTTParty 发出相同的请求,但未能正确构造请求,导致{"statusCode"=>404, "error"=>"Not Found", "message"=>"Not Found"}
返回。我尝试了很多变体,但没有找到正确的结构或参数命名。这是供参考,其中 ENV["X-CMC_PRO_API_KEY"] 是保存我的 api 密钥的环境变量。
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest/'
request = HTTParty.get(url,
{
headers: {
"key": ENV["X-CMC_PRO_API_KEY"],
"Accept": "application/json"
},
data: {
id: 1
},
params: {
slug: self.slug
}
}
)
response = JSON.parse(request.body)
这就是 API 文档建议 cURL 的方式
curl -H "X-CMC_PRO_API_KEY: apikey" -H "Accept: application/json" -d "id=1" -G https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest
这些是来自 POSTMAN 的屏幕,其中请求返回正确的响应。 图片 1 图片2
对于帮助我完成此请求的任何意见,我表示感谢。我是新手,HTTParty 文档没有帮助,我也没有找到任何其他类似的例子。
解决方案
这对我有用
headers = {
'X-CMC_PRO_API_KEY' => "xxxxxxxxxxxxxxxxxxxxx"
}
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest'
params = '?&slug=bitcoin'
response = HTTParty.get(url+params,
:headers => headers
)
推荐阅读
- reactjs - React Typescript 解构 providerValue
- ubuntu - 如何将 SSL 证书添加到 asp.net core docker swarm +letsencrypt?
- javascript - 为什么 JQuery 函数在更新面板中返回未定义
- python - 在没有用户输入的情况下在 python 中存储和使用加密密钥
- python - Numpy - 尝试根据单调条件拆分数组
- javascript - 在反应应用程序中通过 react-bootstrap css 应用自定义字体粗细样式面临困难
- javascript - 应该是交互式的功能,但它只工作一次
- javascript - 为什么计算的模/余数运算符对于大数是错误的?
- emacs - 是否有允许插入一些文本的 Emacs 只读或查看模式?
- python - 无法将 str-casted byte-literal 转换回 str