[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Net::HTTP::Put with 302 redirect?

Chris McMahon

5/21/2007 9:25:00 PM


Sorry if this gets double-posted:

Is there any way to get net/http to follow a 302 redirect?

title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://bar.foo.com:22010">...
a>.</p>

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

require 'net/http'

uri = 'http://foo.bar.com:...

unless uri = (URI.parse(ARGV.shift) rescue nil)
puts "Usage: #$0 <url>"
exit
end

puts "Sending PUT #{uri.request_uri} to #{uri.host}:#{uri.port}"
Net::HTTP.start(uri.host, uri.port) do |http|
headers = {'Content-Type' => 'text/plain; charset=utf-8'}
put_data = "put payload"
response = http.send_request('PUT', uri.request_uri, put_data,
headers)
puts "Response #{response.code} #{response.message}:
#{response.body}"
end

1 Answer

Brian Candler

5/21/2007 9:35:00 PM

0

On Tue, May 22, 2007 at 06:30:03AM +0900, Chris McMahon wrote:
>
> Sorry if this gets double-posted:
>
> Is there any way to get net/http to follow a 302 redirect?

Lots of examples in the source; look for it in /usr/lib/ruby/1.8/net/http.rb
or somewhere similar on your system.

# === Following Redirection
#
# require 'net/http'
# require 'uri'
#
# def fetch(uri_str, limit = 10)
# # You should choose better exception.
# raise ArgumentError, 'HTTP redirect too deep' if limit == 0
#
# response = Net::HTTP.get_response(URI.parse(uri_str))
# case response
# when Net::HTTPSuccess then response
# when Net::HTTPRedirection then fetch(response['location'], limit - 1)
# else
# response.error!
# end
# end
#
# print fetch('http://www.ruby-lan...)