Search code examples
ruby-on-railsrubysmartsheet-api

Replicate curl command to access Smartsheet in Rails


I am trying to access Smartsheet through API 2.0 in a Ruby-on-Rails app. I can get he following curl command to work

curl --request GET https://api.smartsheet.om/2.0/sheets -H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxx"

All attempts to replicate in Ruby code have failed. Can anyone help convert to Ruby?

Thanks!


Solution

  • You have a handful of options to accomplish that. If you want to keep close to curl as you have proposed, you could capture the response of the curl command wraping it with backticks

    response = `curl --request GET https://api.smartsheet.om/2.0/sheets -H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxx"`
    

    I'd recommend, however, use something like CURB, which provides Ruby binding for libcurl. It would add some syntax sugar, so your request would look like

    response = Curl.get("https://api.smartsheet.om/2.0/sheets") do |http|
      http.headers['Authorization'] = 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxx'
    end
    

    Yet another option would be ditching curl altogether and use a gem like HTTParty. It makes use of Ruby's net/http to make requests.

    That would be something along these lines:

    response = HTTParty.get("https://api.smartsheet.om/2.0/sheets",
               headers: 'Authorization' => 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxx')