1. Computing

Discuss in my forum

Getting a Page via a POST Request

By , About.com Guide

Although the primary method of making POST requests with your web browser or Mechanize is by submitting forms, you can also manually make POST requests with the Mechanize#post method.

To make a POST request with no parameters, simply pass the URL to the Mechanize#post method.


agent.post('http://example.com/update')

However, that's not very interesting. To make a POST request with the request body populated with key/value pairs, pass a hash as the second argument.


agent.post('http://example.com/update'
  { :message => "Hello, world" })

Interestingly, Mechanize also supports multipart/form-data POST request (also known as file uploads). If any of the keys in the hash are IO objects, they will be handled correctly.


File.open('message.txt') do|message|
  agent.post('http://example.com/upload/',
             { :message => message })
end

The final parameter to Mechanize#post is a hash of custom parameters, as with Mechanize#get.

  1. About.com
  2. Computing
  3. Ruby
  4. Tutorials
  5. The Mechanize 2.0 Handbook
  6. Getting a Page via a POST Request

©2013 About.com. All rights reserved.