Skip to content

Instantly share code, notes, and snippets.

Created July 2, 2011 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1061645 to your computer and use it in GitHub Desktop.
Save anonymous/1061645 to your computer and use it in GitHub Desktop.
This is an example of using Mechanize 2.0 and posting a form.
require 'rubygems'
require 'mechanize'
require 'net/https'
agent = Mechanize.new
agent.user_agent_alias = 'Mac Safari'
agent.read_timeout = 60
agent.follow_meta_refresh=true
page = agent.get("https://the.website.com/login.do")
current_form=page.forms.first
current_form.username = "myusername"
current_form.password = "mypassword"
newpage = agent.submit current_form
newreq = Mechanize.new
newreq.cookie_jar.add(URI.parse("https://the.website.com/"),agent.cookies[0])
newreq.cookie_jar.add(URI.parse("https://the.website.com/"),agent.cookies[1])
hash = {
"individualIds" => "",
"groupIds" => "",
"publicGroupIds" => "",
"aliasIds" => "",
"phoneNumbers" => "9074339549",
"foreignGroupNames" => "",
"message.parentMsgId" => "",
"to" => "",
"message.subject" => "test subject",
"message.text" => "test message"
}
headers = {"X-Requested-With" => "XMLHttpRequest", "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"}
lastpage = newreq.post("https://the.website.com/pri/messages/do-compose.do?format=json", hash ,headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment