Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Ruby Net::HTTP::Get request with cookie

I'd like to open my stackoverflow.com page via ruby.
And I'd like to see it as if I am authenticated.

I took usr cookie from Google Chrome and created the following snippet:

require 'net/http'
require 'cgi'

url = "http://stackoverflow.com/users/1650525/alex-smolov"
uri = URI(url)
http = Net::HTTP.new(uri.host, 80)
request = Net::HTTP::Get.new(uri.request_uri)

cookie = CGI::Cookie.new("usr", "[my cookie is here]")
request['Cookie'] = cookie
r = http.request(request)
puts r.body

It does output a page, but I'm not authenticated there.

Is it possible to make a Net::HTTP::Get request in Ruby with cookie?

like image 771
Alex Smolov Avatar asked Mar 23 '14 16:03

Alex Smolov


People also ask

How do I send a HTTP request in Ruby?

Ruby comes with a built-in http client, it’s called net/http & you can use it to send any kind of request you need. require 'net/http' Net::HTTP.get ('example.com', '/index.html') This will return a string with the HTML content of the page. But often you want more than the HTML content. Like the HTTP response status.

How do I add cookies to a HTTP request?

# For these methods, such as for QuickGetStr, cookies (or any HTTP request header) # are added by calling SetRequestHeader. http.

How do I make a GET request using HTTPS?

By default Net::HTTP reads an entire response into memory. If you are handling large files or wish to implement a progress bar you can instead stream the body directly to an IO. HTTPS is enabled for an HTTP connection by #use_ssl=. Or if you simply want to make a GET request, you may pass in an URI object that has an HTTPS URL.

How do I Turn on TLS verification in Ruby on http?

Net::HTTP automatically turns on TLS verification if the URI object has a ‘https’ URI scheme. In previous versions of Ruby you would need to require ‘net/https’ to use HTTPS. This is no longer true. Net::HTTP will automatically create a proxy from the http_proxy environment variable if it is present.


2 Answers

You need to call CGI::Cookie.to_s method.

request['Cookie'] = cookie.to_s

Try following code with / without .to_s.

require 'net/http'
require 'cgi'

uri = URI("http://httpbin.org/cookies")
http = Net::HTTP.new(uri.host, 80)
request = Net::HTTP::Get.new(uri.request_uri)
cookie1 = CGI::Cookie.new('usr', 'blah')
request['Cookie'] = cookie1.to_s # <---
r = http.request(request)
puts r.body

UPDATE

As the other answer mentioned, the resulted string is for server output. You need to strip out ; path= part.

CGI::Cookie.new('usr', 'value').to_s.sub(/; path=$/, '')
like image 130
falsetru Avatar answered Sep 19 '22 20:09

falsetru


The accepted answer is imho incorrect. CGI::Cookie#to_s generates string which should SERVER send to client, not something Net::HTTP should use. It can be easily demonstrated:

[1] pry(main)> require 'cgi'
=> true
[2] pry(main)> CGI::Cookie.new('usr', 'value').to_s
=> "usr=value; path="

Code like this should work better.

require 'net/http'
require 'cgi'

uri = URI("http://httpbin.org/cookies")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request['Cookie'] = "usr=#{CGI.encode cookie_value}"
r = http.request(request)
puts r.body

Or in case you have multiple cookies in a hash:

h = {'cookie1' => 'val1', 'cookie2' => 'val2'}
req['Cookie'] = h.map { |k,v| "#{k}=#{CGI.encode v}" } .join('; ')
like image 37
graywolf Avatar answered Sep 20 '22 20:09

graywolf