How do I request a HTTPS page?

I would like to access a https page from irb

Sign in | Etsy

I have been unable to find an example to do this that works.

Is there a good resource on how to do this with Ruby Net::HTTP ?

Does anyone what the general process of this is?

Thank you,

Sam

On Mon, Sep 6, 2010 at 10:40 PM, Samuel S. [email protected]
wrote:

I would like to access a https page from irb

Sign in | Etsy

I have been unable to find an example to do this that works.

Is there a good resource on how to do this with Ruby Net::HTTP ?

Does anyone what the general process of this is?

Thank you,

A simple google search for “ruby https net http” yields:

http://snippets.dzone.com/posts/show/788

maybe you can start from here and make some tests.
Or show us what you have tried and what are the errors?

Jesus.

Samuel S. wrote:

I would like to access a https page from irb

Sign in | Etsy

I have been unable to find an example to do this that works.

Is there a good resource on how to do this with Ruby Net::HTTP ?

What platform are you on?

If it’s Ubuntu or Debian, note that you will be missing lib/net/https.rb
unless you install the libopenssl-ruby package.

From Lucid onwards you can install “ruby-full” which consolidates all
the shrapnel (see below). Note that this will also install emacs as a
dependency unless you do:

sudo apt-get install --no-install-recommends ruby-full

Regards,

Brian.

$ apt-cache show ruby-full
Package: ruby-full
Priority: optional
Section: universe/interpreters
Installed-Size: 32
Maintainer: Ubuntu Core Developers
[email protected]
Original-Maintainer: Fumitoshi UKAI [email protected]
Architecture: all
Source: ruby-defaults
Version: 4.2
Depends: irb, libdbm-ruby, libgdbm-ruby, libopenssl-ruby,
libreadline-ruby, rdoc, ri, ruby, ruby1.8-dev
Recommends: libtcltk-ruby, ruby-elisp
Filename: pool/universe/r/ruby-defaults/ruby-full_4.2_all.deb
Size: 2580
MD5sum: 2124a3d52a2300057e6b2662f770f6d7
SHA1: 6701dd3238dd1a0657c66ff8a98336bf77426f75
SHA256: 0d4c64bc60717dd27a2d6769b7dbbc0d0f0e65ca6bc2f6bc30c496bbb50380a6
Description: Ruby full installation
For many good reasons, the Ruby programming language is split in many
small different packages. Installing this package will make sure you
have
all the packages that add up to a full Ruby installation, with the
exception
of the Tcl/Tk bindings for Ruby, which are only recommended.
.
This package installs the dependencies for the current stable
version of Ruby, which is 1.8.
Homepage: http://www.ruby-lang.org/
Bugs: OpenID transaction in progress
Origin: Ubuntu

$ cat /etc/issue
Ubuntu 10.04.1 LTS

$ uname -a
Linux oso 2.6.32-24-generic #42-Ubuntu SMP Fri Aug 20 14:21:58 UTC 2010
x86_64 GNU/Linux

$ apt-cache show ruby-full
Package: ruby-full
Priority: optional
Section: universe/interpreters
Installed-Size: 32
Maintainer: Ubuntu Core Developers
[email protected]
Original-Maintainer: Fumitoshi UKAI [email protected]
Architecture: all
Source: ruby-defaults
Version: 4.2
Depends: irb, libdbm-ruby, libgdbm-ruby, libopenssl-ruby,
libreadline-ruby, rdoc, ri, ruby, ruby1.8-dev
Recommends: libtcltk-ruby, ruby-elisp
Filename: pool/universe/r/ruby-defaults/ruby-full_4.2_all.deb
Size: 2580
MD5sum: 2124a3d52a2300057e6b2662f770f6d7
SHA1: 6701dd3238dd1a0657c66ff8a98336bf77426f75
SHA256: 0d4c64bc60717dd27a2d6769b7dbbc0d0f0e65ca6bc2f6bc30c496bbb50380a6

That should have been:

sudo apt-get install ruby-full --no-install-recommends

Samuel S. wrote:

$ cat /etc/issue
Ubuntu 10.04.1 LTS

$ uname -a
Linux oso 2.6.32-24-generic #42-Ubuntu SMP Fri Aug 20 14:21:58 UTC 2010
x86_64 GNU/Linux

$ apt-cache show ruby-full

apt-cache shows what packages are available to install, not what are
actually installed. Try:

dpkg-query -l | grep ruby

to see what you have. If you don’t have libopenssl-ruby1.8 then do:

sudo apt-get install ruby-full --no-recommends

Then you can see what files you have:

dpkg-query -L libopenssl-ruby1.8

and this will include /usr/lib/ruby/1.8/net/https.rb

$ dpkg-query -l | grep ruby
ii libopenssl-ruby 4.2
OpenSSL interface for Ruby
ii libopenssl-ruby1.8 1.8.7.249-2
OpenSSL interface for Ruby 1.8
ii libreadline-ruby1.8 1.8.7.249-2
Readline interface for Ruby 1.8
ii libreadline-ruby1.9 1.9.0.5-1ubuntu2
Readline interface for Ruby 1.9
ii libruby1.8 1.8.7.249-2
Libraries necessary to run Ruby 1.8
ii libruby1.9 1.9.0.5-1ubuntu2
Libraries necessary to run Ruby 1.9
ii rake 0.8.7-1
a ruby build program
ii rdoc 4.2
Generate documentation from ruby source file
ii ruby 4.2
An interpreter of object-oriented scripting
ii ruby1.8 1.8.7.249-2
Interpreter of object-oriented scripting lan
ii ruby1.8-dev 1.8.7.249-2
Header files for compiling extension modules
ii ruby1.9 1.9.0.5-1ubuntu2
Interpreter of object-oriented scripting lan
ii rubygems 1.3.5-1ubuntu2
package management framework for Ruby librar
ii rubygems1.8 1.3.5-1ubuntu2
package management framework for Ruby librar
ii rubygems1.9 1.3.5-1ubuntu2
package management framework for Ruby librar

Does

require 'net/https'

work in irb? Then look in /usr/lib/ruby/1.8/net/https.rb for the
documentation, including sample code.

Jesús Gabriel y Galán wrote:

A simple google search for “ruby https net http” yields:

http://snippets.dzone.com/posts/show/788

maybe you can start from here and make some tests.
Or show us what you have tried and what are the errors?

Jesus.

First I will include my code and then 2 responses.

require ‘net/http’
require ‘net/https’
require ‘rubygems’
require ‘oauth’

oauth_consumer_key = “xxx”
oauth_consumer_secret = “yyy”

@consumer = OAuth::Consumer.new(oauth_consumer_key,
oauth_consumer_secret, {:site => “http://openapi.etsy.com”,
:oauth_callback => “oob”, :request_token_path =>
“/v2/sandbox/oauth/request_token”, :authorize_path =>
“/v2/sandbox/oauth/authorize”, :access_token_path =>
“/v2/sandbox/oauth/access_token” })

@request_token = @consumer.get_request_token

http = Net::HTTP.new(‘etsy.com’, 443)
http.use_ssl = true
path =
“oauth/signin?oauth_consumer_key=#{oauth_consumer_key}&oauth_token=#{@request_token.token}”

resp = http.get(path)
puts 'Code = ’ + resp.code
puts 'Message = ’ + resp.message
resp.each {|key, val| puts key + ’ = ’ + val}

done

Here is the response the code yielded:

$ ruby sec.rb
warning: peer certificate won’t be verified in this SSL session
Code = 301
Message = Moved Permanently
location =

expires = Thu, 19 Nov 1981 08:52:00 GMT
connection = close
content-type = text/html; charset=UTF-8
server = Apache
date = Wed, 08 Sep 2010 12:43:15 GMT
content-length = 0
cache-control = no-store, no-cache, must-revalidate, post-check=0,
pre-check=0
pragma = no-cache

###########################################################

I then used the same code except I changed the path variable to include
the ‘https’

path =
https://oauth/signin?oauth_consumer_key=#{oauth_consumer_key}&oauth_token=#{@request_token.token}

Then I received this response:

$ ruby sec.rb
warning: peer certificate won’t be verified in this SSL session
Code = 302
Message = Found
location =
http://etsy.comhttps://etsy.com/oauth/signin?oauth_consumer_key=xxx&oauth_token=yyy
connection = close
server = BigIP
content-length = 0

#end

In the second response the location is weird. It did however produce a
“302”.

In the first response the location says: HTTP, but the url should be
HTTPS

I then used the same code except I changed the path variable to include
the ‘https’

path =
https://oauth/signin?oauth_consumer_key=#{oauth_consumer_key}&oauth_token=#{@request_token.token}

Sorry, I should have wrote:

path =
https://etsy.com/oauth/signin?oauth_consumer_key=#{oauth_consumer_key}&oauth_token=#{@request_token.token}

Brian C. wrote:

Does

require 'net/https'

work in irb? Then look in /usr/lib/ruby/1.8/net/https.rb for the
documentation, including sample code.

Yeah, irb returns true when I require it. It seems like it only works in
a certain order however. Usually it has to be second. I am not sure why.
Thanks for the tip. I didn’t realize that there is documentation there.

Samuel S. wrote:

http = Net::HTTP.new(‘etsy.com’, 443)

The redirect says you should be using www.etsy.com, not etsy.com

http.use_ssl = true
path =
“oauth/signin?oauth_consumer_key=#{oauth_consumer_key}&oauth_token=#{@request_token.token}”

Should have a leading slash on the path. (But not http:// or https://)

If you still get a redirect, then maybe it really wants you to use http
and not https. That is, it seems to be redirecting
https://etsy.com/anyoldjunk to http://etsy.com/anyoldjunk

Brian C. wrote:

Samuel S. wrote:

http = Net::HTTP.new(‘etsy.com’, 443)

The redirect says you should be using www.etsy.com, not etsy.com

http.use_ssl = true
path =
“oauth/signin?oauth_consumer_key=#{oauth_consumer_key}&oauth_token=#{@request_token.token}”

Should have a leading slash on the path. (But not http:// or https://)

If you still get a redirect, then maybe it really wants you to use http
and not https. That is, it seems to be redirecting
https://etsy.com/anyoldjunk to http://etsy.com/anyoldjunk

I added that leading ‘/’ to the path variable. I have also added the www
that you have suggested.

You said that maybe it wants to used http so I tried changing the port
number from 443 to 80.

I get this:

$ruby sec.rb
warning: peer certificate won’t be verified in this SSL session
Code = 302
Message = Moved Temporarily
location =

expires = Thu, 19 Nov 1981 08:52:00 GMT
connection = close
content-type = text/html; charset=UTF-8
date = Wed, 08 Sep 2010 22:27:17 GMT
server = Apache
content-length = 26
content-encoding = gzip
cache-control = no-store, no-cache, must-revalidate, post-check=0,
pre-check=0
pragma = no-cache
��

I am not sure what that last part is. I think it is some kind of
hexidecimal code. Either way, that is what it returned.

Samuel S. wrote:

I added that leading ‘/’ to the path variable. I have also added the www
that you have suggested.

You said that maybe it wants to used http so I tried changing the port
number from 443 to 80.

No, it must be 443 for https. For 80 (http) you would not say use_ssl =
true.

I suggest you keep it at 443 and use_ssl=true, and show what that
response that gives.

Show what it gives in that case. However I think this is no longer a
ruby question, it’s more of an oauth question, so you might get a better
response in an oauth forum.

I am not sure what that last part is. I think it is some kind of
hexidecimal code. Either way, that is what it returned.

Try using inspect, e.g.

resp.each {|key, val| puts key.inspect + ’ = ’ + val.inspect}
puts resp.body.inspect

If you still get that junk, then it’s coming from later on in your
program.

Samuel S. wrote:

Brian C. wrote:

Does

require 'net/https'

work in irb? Then look in /usr/lib/ruby/1.8/net/https.rb for the
documentation, including sample code.

Yeah, irb returns true when I require it. It seems like it only works in
a certain order however

What do you mean by “only works”? If it returns false, that is fine too.
It simply means that the library has already been loaded. If it couldn’t
load it, it would raise an exception.