Forum: Ruby POST with Net::HTTP

A7a0f9b728ed1b4667f168b706de9916?d=identicon&s=25 Ken D'Ambrosio (ravenpi)
on 2014-06-11 21:44
(Received via mailing list)
Hi!  I'm trying to authenticate to an internal application, and I'm
trying to do it using POST.  (I'm also trying to do more stuff than
that, but logging in is kind of a pre-requisite.)

I'm using Net::HTTP -- while in my Googling, I've seen other gems, 1)
they're probably not as portable, and b) I'm trying to learn to do it
from scratch before "cheating."

I've pasted both my code (http://pastebin.com/wamKhC9C) and the form
(http://pastebin.com/0ySiHUG6), but here's a snippet:

------------------------------ cut here ----------------------------

require "net/http"
require "net/https"
require 'uri'

uri = URI.parse("https://racon-vod-bo.example.com/login")
#
?redirect_to=/host/detail/ccdn-ps-em-02.emeryville.ca.oakl3.comcast.net")

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

# Get request to get cookies
#resp, data = http.get("/login", nil)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
cookie = response.response['set-cookie']
puts response.body
puts cookie

request = Net::HTTP::Post.new('https://racon-vod-bo.example.com/login')
request.set_form_data({"username" => "ken", "password" => "Seekrit"})
request['Cookie'] = cookie

------------------------------ cut here ----------------------------

Part of my problem, no doubt, is that I'm trying to gel together several
different components (SSL, POST, cookies) that are all interdependent,
which makes it tricky for me to figure one element out well, and move on
to the next.  I also apologize for the icky code -- I'm just trying to
make it *work* right now; it'll get pretty, later.

Any pointers would be most gratefully accepted.

Thanks!

-Ken
87e61e351b823e19b4a62a896d2af0d4?d=identicon&s=25 Henry Maddocks (Guest)
on 2014-06-11 22:14
(Received via mailing list)
> On 12/06/2014, at 7:43 am, Ken D'Ambrosio <ken@jots.org> wrote:
>
> Hi!  I'm trying to authenticate to an internal application, and I'm trying to do
it using POST.

What's the error?

Henry
A7a0f9b728ed1b4667f168b706de9916?d=identicon&s=25 Ken D'Ambrosio (ravenpi)
on 2014-06-11 22:44
(Received via mailing list)
On 2014-06-11 16:13, Henry Maddocks wrote:
>> On 12/06/2014, at 7:43 am, Ken D'Ambrosio <ken@jots.org> wrote:
>>
>> Hi!  I'm trying to authenticate to an internal application, and I'm
>> trying to do it using POST.
>
> What's the error?

GAH.  I'm sorry.  No actual error -- but when I re-fetch the root page,
I get a login challenge again.  So I'm assuming I'm submitting wrong --
or using cookies wrong.

-Ken
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2014-06-12 05:21
(Received via mailing list)
On Jun 11, 2014, at 13:37, Ken D'Ambrosio <ken@jots.org> wrote:

> GAH.  I'm sorry.  No actual error -- but when I re-fetch the root page, I get a
login challenge again.  So I'm assuming I'm submitting wrong -- or using cookies
wrong.

Use mechanize. This is exactly what it is for.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.