# Posting form data in ruby 1.8.2?

**URL:** <https://www.ruby-forum.com/t/posting-form-data-in-ruby-1-8-2/114582>\
**Category:** Ruby\
**Created:** [September 16, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/posting-form-data-in-ruby-1-8-2/114582 "2007-09-16T12:40:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [September 16, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/posting-form-data-in-ruby-1-8-2/114582/1 "2007-09-16T12:40:42Z")

</div>

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](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:removed\_email\_address@domain.invalid/todo.cgi](http://jack:removed_email_address@domain.invalid/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(“[Yahoo](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(“[Yahoo](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.

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [September 17, 2007, 9:30am UTC](https://www.ruby-forum.com/t/posting-form-data-in-ruby-1-8-2/114582/2 "2007-09-17T09:30:25Z")

</div>

> #req.content\_type = ‘application/x-www-form-urlencoded’

> #I get an error saying there is no content\_type= method.
> 
> #response = Net::HTTP.post\_form(“[Yahoo](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)

🙂 have fun!

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [September 25, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/posting-form-data-in-ruby-1-8-2/114582/3 "2007-09-25T23:00:36Z")

</div>

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/](http://mechanize.rubyforge.org/)

Regards,
