I am attempting to login to a site using the following code:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'csv'
require 'restclient'
HEADERS_HASH = {"User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.5 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.4"}
page = Nokogiri::HTML(open("http://example.com/login", HEADERS_HASH))
token = page.css("form.login_box div input")[0]['value']
login_resp = RestClient.post("https://example.com/session", {"authenticity_token" => token, "login" => 'username', "password" => 'password', "remember_me" => 1, 'commit' => 'Sign In'})
But am getting the following error:
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/abstract_response.rb:39:in `return!': 302 Found (RestClient::Found)
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:230:in `process_result'
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:178:in `block in transmit'
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/1.9.1/net/http.rb:745:in `start'
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient.rb:72:in `post'
from btw.rb:13:in `<main>'
I'm not sure what this error is telling me? Any advice?
Here is the form code:
<form action="https://example.com/session" class="login_box" method="post"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="Vfv9VpDzV2A4Bcgm8III8Gjhf/+GDe1EO7/3qIg+q6I="></div>
<div style="padding:20px;">
<fieldset>
<label for="login">Email</label>
<input id="login" name="login" size="40" tabindex="1" type="text" style="background-image: url(data:image/gif;base64,R0lGODlhEAAQAMQAAHgAFq9RZp8BJfT09JkCJKUuSO/q6/n//vHh5YYBGvf6+tOyucB0hsuLmpUiOpIAHIgJJtzFy7pneuvT2fP49/Dw8L5/juS+x8Scpn4BHaMDJ3cAHHQAG6YDKHEAGv///yH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS4wLWMwNjEgNjQuMTQwOTQ5LCAyMDEwLzEyLzA3LTEwOjU3OjAxICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLyIgeG1wTU06T3JpZ2luYWxEb2N1bWVudElEPSJ4bXAuZGlkOkZCN0YxMTc0MDcyMDY4MTFCRURDRjg2RUEzOUI0MjBEIiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOjUyQjY0RUJGQTQ2QzExRTA5MkM2OTcwNjIwMUM1QjhFIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOjUyQjY0RUJFQTQ2QzExRTA5MkM2OTcwNjIwMUM1QjhFIiB4bXA6Q3JlYXRvclRvb2w9IkFkb2JlIFBob3Rvc2hvcCBDUzUgTWFjaW50b3NoIj4gPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6RkE3RjExNzQwNzIwNjgxMTkyQjA4Nzg0MUEyMjBGMUUiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6RkI3RjExNzQwNzIwNjgxMUJFRENGODZFQTM5QjQyMEQiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz4B//79/Pv6+fj39vX08/Lx8O/u7ezr6uno5+bl5OPi4eDf3t3c29rZ2NfW1dTT0tHQz87NzMvKycjHxsXEw8LBwL++vby7urm4t7a1tLOysbCvrq2sq6qpqKempaSjoqGgn56dnJuamZiXlpWUk5KRkI+OjYyLiomIh4aFhIOCgYB/fn18e3p5eHd2dXRzcnFwb25tbGtqaWhnZmVkY2JhYF9eXVxbWllYV1ZVVFNSUVBPTk1MS0pJSEdGRURDQkFAPz49PDs6OTg3NjU0MzIxMC8uLSwrKikoJyYlJCMiISAfHh0cGxoZGBcWFRQTEhEQDw4NDAsKCQgHBgUEAwIBAAAh+QQAAAAAACwAAAAAEAAQAAAFmGAnjmTZaSgqCCqbautKdMVaOEQsEDkRTAjN44IIyHi8R+DzYRSYjAcy+Ug0PojJZ/HoUh2BgGRwOCga4UK3uiwP3mSmJVFNBBQVQwWuVzASgAkQDmAVFIcShBCAGY0ZAAsHEZEREACOjgABBxQBDhUHFpeNG6UcDhgLHgCpBQClsKUeHBAeGxkctrAcvL2zub2+HsPExcYhADs=); padding-right: 18px; background-attachment: scroll; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(192, 31, 47); border-right-color: rgb(192, 31, 47); border-bottom-color: rgb(192, 31, 47); border-left-color: rgb(192, 31, 47); width: 288px; background-position: 100% 50%; background-repeat: no-repeat no-repeat; ">
</fieldset>
<fieldset>
<label for="password">Password</label>
<input id="password" name="password" size="40" tabindex="2" type="password" style="background-image: url(data:image/gif;base64,R0lGODlhEAAQAMQAAHgAFq9RZp8BJfT09JkCJKUuSO/q6/n//vHh5YYBGvf6+tOyucB0hsuLmpUiOpIAHIgJJtzFy7pneuvT2fP49/Dw8L5/juS+x8Scpn4BHaMDJ3cAHHQAG6YDKHEAGv///yH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS4wLWMwNjEgNjQuMTQwOTQ5LCAyMDEwLzEyLzA3LTEwOjU3OjAxICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLyIgeG1wTU06T3JpZ2luYWxEb2N1bWVudElEPSJ4bXAuZGlkOkZCN0YxMTc0MDcyMDY4MTFCRURDRjg2RUEzOUI0MjBEIiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOjUyQjY0RUJGQTQ2QzExRTA5MkM2OTcwNjIwMUM1QjhFIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOjUyQjY0RUJFQTQ2QzExRTA5MkM2OTcwNjIwMUM1QjhFIiB4bXA6Q3JlYXRvclRvb2w9IkFkb2JlIFBob3Rvc2hvcCBDUzUgTWFjaW50b3NoIj4gPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6RkE3RjExNzQwNzIwNjgxMTkyQjA4Nzg0MUEyMjBGMUUiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6RkI3RjExNzQwNzIwNjgxMUJFRENGODZFQTM5QjQyMEQiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz4B//79/Pv6+fj39vX08/Lx8O/u7ezr6uno5+bl5OPi4eDf3t3c29rZ2NfW1dTT0tHQz87NzMvKycjHxsXEw8LBwL++vby7urm4t7a1tLOysbCvrq2sq6qpqKempaSjoqGgn56dnJuamZiXlpWUk5KRkI+OjYyLiomIh4aFhIOCgYB/fn18e3p5eHd2dXRzcnFwb25tbGtqaWhnZmVkY2JhYF9eXVxbWllYV1ZVVFNSUVBPTk1MS0pJSEdGRURDQkFAPz49PDs6OTg3NjU0MzIxMC8uLSwrKikoJyYlJCMiISAfHh0cGxoZGBcWFRQTEhEQDw4NDAsKCQgHBgUEAwIBAAAh+QQAAAAAACwAAAAAEAAQAAAFmGAnjmTZaSgqCCqbautKdMVaOEQsEDkRTAjN44IIyHi8R+DzYRSYjAcy+Ug0PojJZ/HoUh2BgGRwOCga4UK3uiwP3mSmJVFNBBQVQwWuVzASgAkQDmAVFIcShBCAGY0ZAAsHEZEREACOjgABBxQBDhUHFpeNG6UcDhgLHgCpBQClsKUeHBAeGxkctrAcvL2zub2+HsPExcYhADs=); padding-right: 18px; background-attachment: scroll; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(192, 31, 47); border-right-color: rgb(192, 31, 47); border-bottom-color: rgb(192, 31, 47); border-left-color: rgb(192, 31, 47); width: 288px; background-position: 100% 50%; background-repeat: no-repeat no-repeat; ">
<a href="/forgot_password">Forgot?</a>
</fieldset>
<fieldset class="remember_me">
<input id="remember_me" name="remember_me" tabindex="3" type="checkbox" value="1">
<label for="remember_me">Remember Me</label>
</fieldset>
<input class="login_button" name="commit" tabindex="4" type="submit" value="Sign In">
</div>
</form>
You got a 302 redirect in response to your POST request. RestClient does not handle redirects on POST, so it throws an exception. You need to handle this exception in your code so that you can possibly figure out what the response was or where it is being redirected.
See the RestClient docs here for more information.
You could also use the block-form described here, which seems it does not throw exceptions for POST 302 redirects, and allows you to handle it directly inside the block:
# Follow redirections for all request types and not only for get and head
#
# RFC : "If the 301, 302 or 307 status code is received in response to a
# request other than GET or HEAD, the user agent MUST NOT automatically
# redirect the request unless it can be confirmed by the user, since this
# might change the conditions under which the request was issued."
RestClient.get('http://my-rest-service.com/resource'){ |response, request, result, &block|
if [301, 302, 307].include? response.code
response.follow_redirection(request, result, &block)
else
response.return!(request, result, &block)
end
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With