Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to POST with ruby mechanize

Tags:

ruby

mechanize

I've captured the login HTTP headers using firefox plugin LiveHTTPheaders.

I've found the following url and variables.

POST /login
email=myemail%40gmail.com&password=something&remember=1&loginSubmit=Login

And here's the code I am running:

require 'rubygems'
require 'mechanize'


browser = Mechanize.new
browser.post('http://www.mysite.com/login',
[
["email","myemail%40gmail.com"],
["password","something"],
["remember","1"],
["loginSubmit","Login"],
["url"=>""]
]
) do |page|
puts page.body
end

However, this gives me nothing ! is something wrong with my post parameters ?

like image 340
KJW Avatar asked Aug 28 '10 02:08

KJW


People also ask

What is mechanize in Ruby?

Mechanize is a popular library available with Ruby on Rails to simplify web scraping. It helps to fetch the pages and data that needs to be scrapped. The library automates the interaction with websites by storing cookies, following links, and submitting forms.


1 Answers

post() doesn't take a block. Try this:

page = browser.post('http://www.mysite.com/login', {
  "email" => "myemail%40gmail.com",
  "password" => "something",
  "remember" => "1",
  "loginSubmit" => "Login",
  "url" => ""
})

edit: changed for accuracy

like image 193
cam Avatar answered Sep 28 '22 13:09

cam