Skip to content

Instantly share code, notes, and snippets.

@jagdeepsingh
Last active October 23, 2018 06:17
Show Gist options
  • Save jagdeepsingh/2980ce3684feb4397e566b639a6c2b67 to your computer and use it in GitHub Desktop.
Save jagdeepsingh/2980ce3684feb4397e566b639a6c2b67 to your computer and use it in GitHub Desktop.
HTTP requests using Net::HTTP

HTTP Requests

require 'net/http'
require 'uri'

Parse URL.

uri = URI.parse(url)

Build a Net::HTTP instance.

http = ::Net::HTTP.new(uri.host, uri.port)

Configure the request.

http.use_ssl = true
http.ssl_version = :TLSv1_2
http.ciphers = ['ECDHE-RSA-AES128-GCM-SHA256']
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

Optionally, set the certificate and/or key.

cert = Rails.root.join('path/to/certificate/file.cer').read
http.cert = OpenSSL::X509::Certificate.new(cert)

pem = Rails.root.join('path/to/pem/file.pem').read
http.key = OpenSSL::PKey::RSA.new(pem, nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment