Created
November 5, 2013 02:39
-
-
Save smith/7312959 to your computer and use it in GitHub Desktop.
Making an https request in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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