[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Easy way to do a HTTPS POST?

greg.kujawa

11/11/2006 1:04:00 AM

For a business banking app I needed to script out something that would
upload a PGP-encrypted text file through a HTTPS POST command. Since
Ruby is my language of choice for quick hitters like this I looked into
using it for the HTTPS POST part of the puzzle. After googling around
for awhile I didn't run across anything blatantly obvious as a
solution.

I use the One-Click Installer for Windows version of Ruby 1.8.4 as my
primary development tool. I only saw 'net/http' and not 'net/https' as
valid choices...

4 Answers

Gabriele Marrone

11/11/2006 1:26:00 AM

0


On 11/nov/06, at 02:05, gregarican wrote:

> For a business banking app I needed to script out something that would
> upload a PGP-encrypted text file through a HTTPS POST command. Since
> Ruby is my language of choice for quick hitters like this I looked
> into
> using it for the HTTPS POST part of the puzzle. After googling around
> for awhile I didn't run across anything blatantly obvious as a
> solution.
>
> I use the One-Click Installer for Windows version of Ruby 1.8.4 as my
> primary development tool. I only saw 'net/http' and not 'net/https' as
> valid choices...

Net::HTTP does the job.
You can see examples on http://www.ruby-doc.org/stdlib/l...
http/rdoc/classes/Net/HTTP.html , I'm copypasting here the POST ones:

require 'net/http'
require 'uri'

#1: Simple POST
res = Net::HTTP.post_form(URI.parse('http://www.ex...
search.cgi'),
{'q'=>'ruby', 'max'=>'50'})
puts res.body

#2: POST with basic authentication
res = Net::HTTP.post_form(URI.parse('http://
jack:pass@www.example.com/todo.cgi'),
{'from'=>'2005-01-01',
'to'=>'2005-03-31'})
puts res.body

#3: Detailed control
url = URI.parse('http://www.ex...todo.cgi')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'jack', 'pass'
req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
res = Net::HTTP.new(url.host, url.port).start {|http|
http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.error!
end

If you need HTTPS make sure to require 'net/https' and use something
like this:

#3: Detailed control
url = URI.parse('http://www.ex...todo.cgi')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'jack', 'pass'
req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
http = Net::HTTP.new(url.host, url.port) # lines
http.use_ssl = true # modified
res = http.start {|http| http.request(req) } # by me
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.error!
end

I didn't actually test this code but I think it should work.

By the way, it doesn't support cookies as far as I know.
In fact I needed a library with which I could interact with a website
that needed a session id stored in a cookie (and not encoded within
the URL) and I wasn't able to find any. :(


--
Gabriele Marrone



Gabriele Marrone

11/11/2006 1:35:00 AM

0


On 11/nov/06, at 02:25, Gabriele Marrone wrote:

> By the way, it doesn't support cookies as far as I know.

Oops! As soon as I posted, I found an example about using cookies
with Net::HTTP...
Ok, nevermind :)

--
Gabriele Marrone



greg.kujawa

11/11/2006 1:41:00 AM

0

Gotcha. I guess since most of the API docs just had HTTP examples I
assumed that 'net/http' didn't support HTTPS. Since it does this should
be a snap. Thanks for the quick replies!

Gabriele Marrone wrote:
> On 11/nov/06, at 02:05, gregarican wrote:
>
> > For a business banking app I needed to script out something that would
> > upload a PGP-encrypted text file through a HTTPS POST command. Since
> > Ruby is my language of choice for quick hitters like this I looked
> > into
> > using it for the HTTPS POST part of the puzzle. After googling around
> > for awhile I didn't run across anything blatantly obvious as a
> > solution.
> >
> > I use the One-Click Installer for Windows version of Ruby 1.8.4 as my
> > primary development tool. I only saw 'net/http' and not 'net/https' as
> > valid choices...
>
> Net::HTTP does the job.
> You can see examples on http://www.ruby-doc.org/stdlib/l...
> http/rdoc/classes/Net/HTTP.html , I'm copypasting here the POST ones:
>
> require 'net/http'
> require 'uri'
>
> #1: Simple POST
> res = Net::HTTP.post_form(URI.parse('http://www.ex...
> search.cgi'),
> {'q'=>'ruby', 'max'=>'50'})
> puts res.body
>
> #2: POST with basic authentication
> res = Net::HTTP.post_form(URI.parse('http://
> jack:pass@www.example.com/todo.cgi'),
> {'from'=>'2005-01-01',
> 'to'=>'2005-03-31'})
> puts res.body
>
> #3: Detailed control
> url = URI.parse('http://www.ex...todo.cgi')
> req = Net::HTTP::Post.new(url.path)
> req.basic_auth 'jack', 'pass'
> req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
> res = Net::HTTP.new(url.host, url.port).start {|http|
> http.request(req) }
> case res
> when Net::HTTPSuccess, Net::HTTPRedirection
> # OK
> else
> res.error!
> end
>
> If you need HTTPS make sure to require 'net/https' and use something
> like this:
>
> #3: Detailed control
> url = URI.parse('http://www.ex...todo.cgi')
> req = Net::HTTP::Post.new(url.path)
> req.basic_auth 'jack', 'pass'
> req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
> http = Net::HTTP.new(url.host, url.port) # lines
> http.use_ssl = true # modified
> res = http.start {|http| http.request(req) } # by me
> case res
> when Net::HTTPSuccess, Net::HTTPRedirection
> # OK
> else
> res.error!
> end
>
> I didn't actually test this code but I think it should work.
>
> By the way, it doesn't support cookies as far as I know.
> In fact I needed a library with which I could interact with a website
> that needed a session id stored in a cookie (and not encoded within
> the URL) and I wasn't able to find any. :(
>
>
> --
> Gabriele Marrone

Aaron Patterson

11/12/2006 8:43:00 PM

0

On Sat, Nov 11, 2006 at 10:25:38AM +0900, Gabriele Marrone wrote:
>
> On 11/nov/06, at 02:05, gregarican wrote:
>
> >For a business banking app I needed to script out something that would
> >upload a PGP-encrypted text file through a HTTPS POST command. Since
> >Ruby is my language of choice for quick hitters like this I looked
> >into
> >using it for the HTTPS POST part of the puzzle. After googling around
> >for awhile I didn't run across anything blatantly obvious as a
> >solution.
> >
> >I use the One-Click Installer for Windows version of Ruby 1.8.4 as my
> >primary development tool. I only saw 'net/http' and not 'net/https' as
> >valid choices...
>
> Net::HTTP does the job.
> You can see examples on http://www.ruby-doc.org/stdlib/l...
> http/rdoc/classes/Net/HTTP.html , I'm copypasting here the POST ones:
>
> require 'net/http'
> require 'uri'
>
> #1: Simple POST
> res = Net::HTTP.post_form(URI.parse('http://www.ex...
> search.cgi'),
> {'q'=>'ruby', 'max'=>'50'})
> puts res.body
>
> #2: POST with basic authentication
> res = Net::HTTP.post_form(URI.parse('http://
> jack:pass@www.example.com/todo.cgi'),
> {'from'=>'2005-01-01',
> 'to'=>'2005-03-31'})
> puts res.body
>
> #3: Detailed control
> url = URI.parse('http://www.ex...todo.cgi')
> req = Net::HTTP::Post.new(url.path)
> req.basic_auth 'jack', 'pass'
> req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
> res = Net::HTTP.new(url.host, url.port).start {|http|
> http.request(req) }
> case res
> when Net::HTTPSuccess, Net::HTTPRedirection
> # OK
> else
> res.error!
> end
>
> If you need HTTPS make sure to require 'net/https' and use something
> like this:
>
> #3: Detailed control
> url = URI.parse('http://www.ex...todo.cgi')
> req = Net::HTTP::Post.new(url.path)
> req.basic_auth 'jack', 'pass'
> req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
> http = Net::HTTP.new(url.host, url.port) # lines
> http.use_ssl = true # modified
> res = http.start {|http| http.request(req) } # by me
> case res
> when Net::HTTPSuccess, Net::HTTPRedirection
> # OK
> else
> res.error!
> end
>
> I didn't actually test this code but I think it should work.
>
> By the way, it doesn't support cookies as far as I know.
> In fact I needed a library with which I could interact with a website
> that needed a session id stored in a cookie (and not encoded within
> the URL) and I wasn't able to find any. :(

Try Mechanize. It will handle redirects and cookies for you. It will
also post http/https forms.

http://mechanize.ruby...

>
>
> --
> Gabriele Marrone
>
>
>

--
Aaron Patterson
http://tenderlovem...