# Net::HTTP::Put with 302 redirect?

**URL:** <https://www.ruby-forum.com/t/net-put-with-302-redirect/104735>\
**Category:** Ruby\
**Created:** [May 21, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/net-put-with-302-redirect/104735 "2007-05-21T23:31:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_McMahon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@Chris\_McMahon](https://www.ruby-forum.com/u/Chris_McMahon)\
**Post date:** [May 21, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/net-put-with-302-redirect/104735/1 "2007-05-21T23:31:15Z")

</div>

Sorry if this gets double-posted:

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

title\>302 Found

# Found

The document has moved [here.](http://bar.foo.com:22010)

[ 

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

 

require ‘net/http’

 ](http://bar.foo.com:22010)

[uri = ‘](http://bar.foo.com:22010)[http://foo.bar.com:22010](http://foo.bar.com:22010)’

unless uri = (URI.parse(ARGV.shift) rescue nil)  
puts “Usage: #$0 ”  
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

---

<div class="post-metadata">

**Author:** ![Chris\_McMahon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@Chris\_McMahon](https://www.ruby-forum.com/u/Chris_McMahon)\
**Post date:** [May 21, 2007, 11:36pm UTC](https://www.ruby-forum.com/t/net-put-with-302-redirect/104735/2 "2007-05-21T23:36:16Z")

</div>

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-lang.org](http://www.ruby-lang.org)’)
