简体   繁体   English

将curl命令转换为httparty

[英]Translating a curl command to httparty

I want to consume some API data from a Rails app. 我想从Rails应用程序中使用一些API数据。 A curl example is curl --data 'api_key=your_api_key&api_secret=your_api_secret&host_id=your_user_host_id' https://api.zoom.us/v1/webinar/list I have experimented with this at the terminal and I am seeing expected responses. 一个卷曲的例子是curl --data 'api_key=your_api_key&api_secret=your_api_secret&host_id=your_user_host_id' https://api.zoom.us/v1/webinar/list我在终端进行了实验,我看到了预期的回复。 I'm now experimenting in a ruby script using httparty. 我现在正在使用httparty尝试使用ruby脚本。 My question is how should I handle the 'stuff' before the endpoint (api_key…secret…ect)? 我的问题是如何在端点之前处理'stuff'(api_key ... secret ... ect)? Are these headers? 这些标题是?

In regard to curl --data only tells me that it is a post request, but I'm not sure how that translates to httparty. 关于curl --data只告诉我这是一个帖子请求,但我不确定这是如何转换为httparty。

Here is a first attempt: 这是第一次尝试:

require 'httparty'

api_key = 'myKey'
api_secret = 'secret'
host_id = 'host'
data_type = 'JSON'

response = HTTParty.post("api_key&api_secret&host_id&data_type https://api.zoom.us/v1/webinar/list/registration")

puts response.parsed_response

But this gives me a bad URI response. 但这给了我一个糟糕的URI响应。 If I run this same script with the endpoint only I do get a response code back from zoom saying that API key and secret are required. 如果我只使用端点运行相同的脚本,我会从缩放中获得一个响应代码,说明需要API密钥和密钥。

Looking at this example I think this should work: 看看这个例子,我认为这应该有效:

require 'httparty'

api_key = 'myKey'
api_secret = 'secret'
host_id = 'host'
data_type = 'JSON'

options = {
  body: {
    api_key: api_key,
    api_secret: api_secret,
    host_id: host_id,
    data_type: data_type
  }
}
response = HTTParty.post("https://api.zoom.us/v1/webinar/list/registration", options)

puts response.parsed_response

I get the response: 我收到了回复:

{"error"=>{"code"=>200, "message"=>"Invalid api key or secret."}} {“error”=> {“code”=> 200,“message”=>“无效的api密钥或密码。”}}

which I think is a step in the right direction. 我认为这是朝着正确方向迈出的一步。

No those are not headers those are parameters. 没有那些不是标题那些是参数。 Header are usually denoated by the -H flag. 标头通常由-H标志去除。

Try this: 试试这个:

require 'httparty'

query_params = {api_key: 'myKey',
                api_secret: 'secret',
                host_id: 'host',
                data_type: 'JSON'}

response = HTTParty.post("api_key&api_secret&host_id&data_type https://api.zoom.us/v1/webinar/list/registration", :query => query_params)

puts response.parsed_response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM