Get HTTPS response - ruby

This works great:
require 'net/http'
uri = URI('http://api.twitter.com/1/statuses/user_timeline.json')
args = {include_entities: 0, include_rts: 0, screen_name: 'johndoe', count: 2, trim_user: 1}
uri.query = URI.encode_www_form(args)
resp = Net::HTTP.get_response(uri)
puts resp.body
But changing from http to https leads to a meaningless error. I'm not asking why the error is meaningless, I would just like to know what's the closest means of doing get_response for https?
I've seen the 'HTTPS' example in the Net::HTTP doc but it looks not very impressive and will make me manually compose the URL from my parameters hash - no good.

Here is a example which works for me under Ruby 1.9.3
require "net/http"
uri = URI.parse("https://api.twitter.com/1/statuses/user_timeline.json")
args = {include_entities: 0, include_rts: 0, screen_name: 'johndoe', count: 2, trim_user: 1}
uri.query = URI.encode_www_form(args)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
response.body

Related

uninitialized constant Net::HTTPS (NameError)

I am trying to pull campaign stats from Clickbank API in ruby. When I run the sample code Clickbank provided. I get the following error:
uninitialized constant Net::HTTPS (NameError). What am I missing?
Example Code.
require 'net/http'
require 'net/https'
http = Net::HTTPS.new('api.clickbank.com')
http.use_ssl = false
path = '/rest/1.3/orders/list'
headers = {
'Authorization' => '<< DEVKEY >>:<< APIKEY>>',
'Accept' => 'application/json'
}
resp, data = http.get(path, nil, headers)
puts 'Code = ' + resp.code
puts 'Message = ' + resp.message
resp.each {|key, val| puts key + ' = ' + val}
puts data
Yes I put my dev and api key into
In Ruby 2.4.1 enable ssl as a parameter of Net::HTTP.start
Net::HTTP.start(uri.host, uri.port, use_ssl: true)
https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-HTTPS
Use Net:HTTP and enable SSL instead of using Net::HTTPS and disabling SSL.
Example:
http = Net::HTTP.new('api.clickbank.com')
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
You actually don't want to disable ssl as that API requires it. I was able to get it working like so based on the documentation for http found here: http://ruby-doc.org/stdlib-2.1.1/libdoc/net/http/rdoc/Net/HTTP.html
require 'net/http'
uri = URI('https://api.clickbank.com/rest/1.3/orders/list')
req = Net::HTTP::Get.new(uri)
# set headers on the request
req['Authorization'] = '<< DEVKEY >>:<< APIKEY>>'
req['Accept'] = 'application/json'
# perform the request
resp, data = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
puts 'Code = ' + resp.code
puts 'Message = ' + resp.message
resp.each {|key, val| puts key + ' = ' + val}
puts data

How to get response OK from POP3 server using ruby

For example, to get response 200 OK from "example.com", necessary:
require 'net/http'
uri = URI('http://example.com/index.html')
res = Net::HTTP.get_response(uri)
puts res.code # => '200'
puts res.message # => 'OK'
How to make similar for pop.gmail.com?
Try this:
require "net/pop"
Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
conn = Net::POP3.new("pop.gmail.com", 995)
conn.start(user_name, password)
conn.started?

String interpolation and URI.parse error?

I have four arguments taken from user input in the script. All other string interpolation arguments work fine except for URI.parse.
Snippets from the code are:
require 'net/http'
#ARGVs:
prompt = 'test: '
url = ARGV[0]
user = ARGV[1]
pass = ARGV[2]
xml_user = ARGV[3]
# User supplied input:
puts "Whats the URL?"
print prompt
url = STDIN.gets.chomp()
# HTTP connection
uri = URI.parse('#{url}')
req = Net::HTTP.new(uri.hostname, uri.port)
# Header: Creds to get a session
user_and_pass = "#{user}" + ':' + "#{pass}"
base64user_and_pass = Base64.encode64(user_and_pass)
# POST method
res = req.post(uri.path, xml_data, {'Content-Type' => 'text/xml', 'Content-Length' => xml_data.length.to_s,
'Authorization' => "Basic #{base64user_and_pass}", "Connection" => "keep-alive" })
puts res.body
Error:
Ruby200-x64/lib/ruby/2.0.0/uri/common.rb:176:in `split': bad URI(is not URI?): #{url} (URI::InvalidURIError)
A point in the right direction would be appreciated.
uri = URI.parse('#{url}')
should be:
uri = URI.parse(url)
Here's a bit more idiomatic-Ruby version of the code:
require 'net/http'
PROMPT = 'test: '
# ARGVs:
url, user, pass, xml_user = ARGV[0, 4]
# User supplied input:
puts "Whats the URL?"
print PROMPT
url = STDIN.gets.chomp()
# HTTP connection
uri = URI.parse(url)
req = Net::HTTP.new(uri.hostname, uri.port)
# Header: Creds to get a session
base64user_and_pass = Base64.encode64("#{ user }:#{ pass }")
# POST method
res = req.post(
uri.path,
xml_data,
{
'Content-Type' => 'text/xml',
'Content-Length' => xml_data.length.to_s,
'Authorization' => "Basic #{ base64user_and_pass }",
"Connection" => "keep-alive"
}
)
puts res.body
Don't do things like:
user_and_pass = "#{user}" + ':' + "#{pass}"
and:
uri = URI.parse('#{url}')
user, pass and url are already strings, so sticking them inside a string and interpolating their values is a waste of CPU. As developers we need to be aware of our data-types.
It could be written as one of these:
user_and_pass = user + ':' + pass
user_and_pass = '%s:%s' % [user, pass]
user_and_pass = [user, pass].join(':')
But it's more idiomatic to see it how I wrote it above.

Cannot get URL response via Ruby

I have an error connecting in Ruby to the URL listed below, even though the URL exists. Why is that?
1.9.3p194 :003 > require 'uri'
=> true
1.9.3p194 :004 > require 'net/http'
=> true
1.9.3p194 :005 > url = "https://blogs.oracle.com/ksplice/entry/introducing_redpatch"
=> "https://blogs.oracle.com/ksplice/entry/introducing_redpatch"
1.9.3p194 :006 > url_parsed = URI.parse(url)
=> #<URI::HTTPS:0x00000001939288 URL:https://blogs.oracle.com/ksplice/entry/introducing_redpatch>
1.9.3p194 :007 > response = Net::HTTP.get_response(url_parsed)
Errno::ECONNRESET: Connection reset by peer
Rather than use Net::HTTP, simplify your life and use Ruby's OpenURI. Unless you need low-level control or visibility of low-level values, you'll find OpenURI is good enough:
require 'open-uri'
url = "https://blogs.oracle.com/ksplice/entry/introducing_redpatch"
open(url).read.size
=> 35493
Use this
url = "https://blogs.oracle.com/ksplice/entry/introducing_redpatch"
uri = URI.parse(url)
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)
response = http.request(request)
response.body
It's taken from here: http://www.rubyinside.com/nethttp-cheat-sheet-2940.html

Ruby: Blocking HTTP.new.start and dynamic HTTP(S)

I need an application to block an HTTP request so I had to add a couple of lines of code, the only piece I couldn't figure out was the statement if uri.scheme == 'https'; http.use_ssl = true is there a way I can set http/https in the current statement:
Net::HTTP.new(uri.host, uri.port).start do |http|
# Causes and IOError...
if uri.scheme == 'https'
http.use_ssl = true
end
request = Net::HTTP::Get.new(uri.request_uri)
http.request(request)
end
Added: IOError: use_ssl value changed, but session already started
Per the documentation, you cannot call use_ssl= after starting the session (i.e. after start). You have to set it before, e.g.:
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'
http.start do |h|
h.request Net::HTTP::Get.new(uri.request_uri)
end
Try to change your code to
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme =='https'
http.use_ssl = true
end
http.start do
request = Net::HTTP::Get.new(uri.request_uri)
puts http.request(request)
end

Resources