Lumbee Lumbee - 1 month ago 12
Ruby Question

Translating a curl command to httparty

I want to consume some API data from a Rails app. 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. I’m now experimenting in a ruby script using httparty. My question is how should I handle the ‘stuff’ before the endpoint (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.

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. 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.

Answer

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."}}

which I think is a step in the right direction.