Recently I was using Mechanize for this kind of thing, but I want to use Typhoeus, which I'm already using everywhere else. I want to mimic Mechanize's behavior, the issue is that I would like to log in into a site and perform requests as logged in user. Here is generalised version of the script:
require 'rubygems'
require 'typhoeus'
GET_URL = 'http://localhost:3000'
POST_URL = "http://localhost:3000/admins/sign_in"
URL = "http://localhost:3000/dashboard"
USERNAME_FIELD = 'admin[email]'
PASSWORD_FIELD = 'admin[password]'
USERNAME = "admin@example.com"
PASSWORD = "my_secret_password"
def merge_cookies_into_cookie_jar(response)
if response.headers_hash['set-cookie'].instance_of? Array
response.headers_hash['set-cookie'].each do |cookie|
@cookie_jar << cookie.split('; ')[0]
end
elsif response.headers_hash['set-cookie'].instance_of? String
@cookie_jar << response.headers_hash['set-cookie'].split('; ')[0]
end
end
# initialize cookie jar
@cookie_jar = []
# for server to establish me a session
response = Typhoeus::Request.get( GET_URL, :follow_location => true )
merge_cookies_into_cookie_jar(response)
# like submiting a log in form
response = Typhoeus::Request.post( POST_URL,
:params => { USERNAME_FIELD => USERNAME, PASSWORD_FIELD => PASSWORD },
:headers => { 'Cookie' => @cookie_jar.join('; ') }
)
merge_cookies_into_cookie_jar(response)
# the page I'd like to get in a first place,
# but not working, redirects me back to login form with 401 Unauthorized :-(
response = Typhoeus::Request.get( URL,
:follow_location => true,
:headers => { 'Cookie' => @cookie_jar.join('; ') }
)
The cookie gets sent to the server, but for some reason I'm not logged in. I tested it on two different sites (which one of them was my Rails application's administration). Any idea what am I doing wrong or maybe a better or more widely applicable solution to this problem?