From: "Christopher Özbek" Date: 2002-04-05T07:53:02+09:00 Subject: Re: [net/http] how to post form data ? Hi! I found this escape function for post requests in a module somewhere... Solved most of my problems with post... If there is a way to do this with less code (a module?), any help would be great. Christopher require 'net/http' def escape(str, unsafe=nil) if not unsafe str.gsub(/[^;\/?:@&=+$,0-9A-Za-z\-_.!~*'()\[\]]/) do |match| #' sprintf('%%%02X', match.unpack('C')[0]) end else str.gsub (/[#{unsafe}]/) do |match| sprintf('%%%02 X', match.unpack('C')[0]) end end end def unpack(attr, value, &block) if value.kind_of?(Array) or value.kind_of?(Hash) then value.each do |value| unpack(attr, value, &block) end else block.call(attr, value) end end def escape_query(query) data = '' query.each do |attr, value| unpack(attr, value) do |attr, value| data << '&' if !data.empty? data << escape( attr.to_s ) << '=' << escape( value.to_s ) end end data end def post(site, link, post_hash) h = Net::HTTP.new(site, 80) s = escape_query(post_hash) puts s resp, data = h.post(link, s, nil ) if resp.code.to_i != 200 then raise "Error #{resp.code} \"#{resp.message}\" occurred" end data end aHash = 'P' => 'amg', 'uid' => 'search', 'sql' => '2', 'opt1' => 'Secret Samadhi' } link = '/cg/amg.dll' site = 'www.allmusic.com' data = post(site, link, aHash) puts data "FreeMan" schrieb im Newsbeitrag news:a8htje$14qc$1@goldorak.iie.cnam.fr... > here is what i do : > > --BEGIN-- > > require 'net/http' > include Net > > $host = 'localhost' > $path = '/test.php' > > > HTTP::Proxy($proxy_addr,$proxy_port).start($host) { |http| > $topost = 'b=2' > puts 'posting : '+$topost > response , = http.post($path,$topost) > puts response.body > } > > --END-- > > test.php is a simple php script that shows all the GET/POST variables > > the output of the script is : > > --BEGIN-- > > posting : b=2 > Array > ( > ) > Array > ( > ) > > --END-- > > what's wrong ? > > thx >