Posting form data in ruby 1.8.2?

Hi,

I’m trying to figure out how to post form data to a url, but the methods
used in the examples in the Net::HTTP docs use methods that aren’t
available in my ruby install:

Posting Form Data:

require 'net/http'
require 'uri'

#1: Simple POST
res =

Net::HTTP.post_form(URI.parse(‘http://www.example.com/search.cgi’),
{‘q’=>‘ruby’, ‘max’=>‘50’})
puts res.body

#2: POST with basic authentication
res =

Net::HTTP.post_form(URI.parse(‘http://jack:[email protected]/todo.cgi’),
{‘from’=>‘2005-01-01’,
‘to’=>‘2005-03-31’})
puts res.body

#3: Detailed control
url = URI.parse('http://www.example.com/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

I looked at the source code for the methods that cause missing method
errors, and I tried to use the lower level methods used in the source,
but some of them weren’t available either, which required that I look
into more source files, and eventually I got lost in a labyrinth. Here
are my feeble beginnings:

require “net/http”
require “uri”

url = URI.parse("https://login.yahoo.com/config/login?")
puts url.path
puts url.port

req = Net::HTTP::Post.new(url.path)
#req.content_type = ‘application/x-www-form-urlencoded’
#req.body = “username=tailing_loop2003&passwd=bwopmd”
#I get an error saying there is no content_type= method.

#response = Net::HTTP.post_form("https://login.yahoo.com/config/login?",
{“username”, “aaaaaa”, “passwd”, “bbbb”})
#No post_form method.

Is there an example online that I can look at somewhere? I can’t find
anything in pickaxe2 or ruby way2.

#req.content_type = ‘application/x-www-form-urlencoded’

#I get an error saying there is no content_type= method.

#response = Net::HTTP.post_form("https://login.yahoo.com/config/login?",
{“username”, “aaaaaa”, “passwd”, “bbbb”})
#No post_form method.

content_type should be in () IE: content_type=(type, params = {})
instead of quotes

same type of scenario for no post_form method

Net::HTTP::post_form(url, params)

:slight_smile: have fun!

7stud – wrote:

Hi,

Hi.

Is there an example online that I can look at somewhere? I can’t find
anything in pickaxe2 or ruby way2.

Try Mechanize: http://mechanize.rubyforge.org/

Regards,

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs