Skip to content

Instantly share code, notes, and snippets.

@smith
Created November 5, 2013 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smith/7312959 to your computer and use it in GitHub Desktop.
Save smith/7312959 to your computer and use it in GitHub Desktop.
Making an https request in ruby
string_uri = 'https://api.opscode.piab/organizations/test/nodes'
# Using Net::HTTP
require 'net/http'
require 'openssl'
require 'uri'
uri = URI.parse('https://api.opscode.piab/organizations/test/nodes')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
request['X-Ops-UserId'] = 'pivotal'
response = http.request(request)
p api_info = response['x-ops-api-info']
# Using RestClient (a dependency of chef)
require 'restclient'
begin
RestClient.get(string_uri, { 'X-Ops-UserId' => 'pivotal' })
rescue RestClient::BadRequest => e
response = e.response
end
p api_info = response.headers[:x_ops_api_info]
# Using Excon (asset sync depends on fog, which depends on excon. since asset_syn is only in the assets gem group, this may not currently work in prod)
require 'excon'
Excon.defaults[:ssl_verify_peer] = false
response = Excon.get(string_uri, { :headers => { 'X-Ops-UserId' => 'pivotal' } })
p api_info = response.headers['X-Ops-API-Info']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment