POST with Net::HTTP

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

What's the error?

Henry

···

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.

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

···

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?

Use mechanize. This is exactly what it is for.

···

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.