How to implement ruby ​​net / http cookie support?

I want to add cookie support to the ruby ​​class using net / http to browse the web. Cookies must be stored in the file in order to survive after the script completes. Of course, I can read the specifications and write some kind of handler, use some cookie.txt format, etc., but it seems like it means reinventing the wheel. Is there a better way to accomplish this task? Maybe some cooie jar class for cookie care?

+43
ruby cookies
Sep 28 '09 at 12:06
source share
6 answers

Taken from Dzone Snippets

http = Net::HTTP.new('profil.wp.pl', 443) http.use_ssl = true path = '/login.html' # GET request -> so the host can set his cookies resp, data = http.get(path, nil) cookie = resp.response['set-cookie'].split('; ')[0] # POST request -> logging in data = 'serwis=wp.pl&url=profil.html&tryLogin=1&countTest=1&logowaniessl=1&login_username=blah&login_password=blah' headers = { 'Cookie' => cookie, 'Referer' => 'http://profil.wp.pl/login.html', 'Content-Type' => 'application/x-www-form-urlencoded' } resp, data = http.post(path, data, headers) # Output on the screen -> we should get either a 302 redirect (after a successful login) or an error page puts 'Code = ' + resp.code puts 'Message = ' + resp.message resp.each {|key, val| puts key + ' = ' + val} puts data 

Update

 #To save the cookies, you can use PStore cookies = PStore.new("cookies.pstore") # Save the cookie cookies.transaction do cookies[:some_identifier] = cookie end # Retrieve the cookie back cookies.transaction do cookie = cookies[:some_identifier] end 
+31
Sep 28 '09 at 12:26
source
— -

The accepted answer will not work if your server returns and expects multiple cookies. This can happen, for example, if the server returns a set of FedAuth cookies [n]. If this affects you, you may need to use something in the following lines instead:

 http = Net::HTTP.new('https://example.com', 443) http.use_ssl = true path1 = '/index.html' path2 = '/index2.html' # make a request to get the server cookies response = http.get(path) if (response.code == '200') all_cookies = response.get_fields('set-cookie') cookies_array = Array.new all_cookies.each { | cookie | cookies_array.push(cookie.split('; ')[0]) } cookies = cookies_array.join('; ') # now make a request using the cookies response = http.get(path2, { 'Cookie' => cookies }) end 
+37
Feb 16 2018-12-12T00:
source

The accepted answer does not work. You need to access the internal representation of the response header, where the multiple cookie values ​​are stored separately, and then delete everything after the first semicolon from this line and combine them together. Here is the code that works

 r = http.get(path) cookie = {'Cookie'=>r.to_hash['set-cookie'].collect{|ea|ea[/^.*?;/]}.join} r = http.get(next_path,cookie) 
+12
May 11 '12 at 4:29
source

Use an http cookie that implements RFS-compliant parsing and rendering, plus a jar.

A rough example that happens after a redirect after login:

 require 'uri' require 'net/http' require 'http-cookie' uri = URI('...') jar = HTTP::CookieJar.new Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| req = Net::HTTP::Post.new uri req.form_data = { ... } res = http.request req res.get_fields('Set-Cookie').each do |value| jar.parse(value, req.uri) end fail unless res.code == '302' req = Net::HTTP::Get.new(uri + res['Location']) req['Cookie'] = HTTP::Cookie.cookie_value(jar.cookies(uri)) res = http.request req end 

What for? Since the answers above are incredibly inadequate and flat, they don’t work in many scenarios related to RFC (this happened to me), so relying on lib itself using just what you need is infinitely more reliable if you want to process more one specific case.

+5
Sep 30 '15 at 14:21
source

I used Curb and Mechanize for a similar project. Just enable cookies and save cookies in temp cookiejar ... If you use the built-in network / http or packages without cookies, you will need to write your own cookie handling.

+3
Sep 28 '09 at 17:35
source

You can send cookies using headers.

You can save the title in any save structure. Be it some kind of database or files.

+1
Sep 28 '09 at 12:21
source



All Articles