# Handling cookies with Net::HTTP?

**URL:** <https://www.ruby-forum.com/t/handling-cookies-with-net-http/98488>\
**Category:** Ruby\
**Created:** [July 15, 2006, 12:38am UTC](https://www.ruby-forum.com/t/handling-cookies-with-net-http/98488 "2006-07-15T00:38:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 15, 2006, 12:38am UTC](https://www.ruby-forum.com/t/handling-cookies-with-net-http/98488/1 "2006-07-15T00:38:18Z")

</div>

I am having a strange problem and it is partly due to my poor  
understanding of cookies. So any help is greatly appreciated.

I am trying to navigate through a site which requires that I provide  
them with the cookies they send me. I am having some trouble here. I  
am grabbing the ‘set-cookie’ header they send back to me and storing  
each cookie in a hash like: cookies[name] = value. Here is the code I  
wrote for that:

def parse\_cookie(set\_cookie)  
# Attempt to pull out SID, BID, CID  
@cookies ||= {}  
[‘SID’, ‘BID’, ‘CID’].each do |var|  
cookie = set\_cookie.scan(/(#{var}=.\*?);/)  
cookie = cookie.join(’’).split(’=’)  
@cookies[cookie[0]] = cookie[1] unless cookie.size != 2  
end  
end

I only want the SID, BID, and CID cookies.

Then when I send another request to them I am doing the following:

def cookies  
cookie\_str = ‘’  
@cookies.each do |k, v|  
unless cookie\_str == ‘’  
cookie\_str \<\< ‘;’  
end  
cookie\_str \<\< k \<\< ‘=’ \<\< v  
end unless @cookies.nil?  
cookie\_str  
end

req.add\_field ‘Cookie’, cookies #this calls the above method

Which ended up sending the following ‘Cookie’ header:

SID=VpI2zrrUjHDufXLqUec5VEWPia-JgWWQH3k-  
DPOxEhOKcvtZh46eZxw2xrAwK5N645v3B4oRAtoI0b8\_;BID=bWtbrEQbrPcZmyvFQoXsZoq  
qWYrhV81w1698hx5-e1NUQZqE-tg9K9K36pt\_CFIXcuW-  
FOkplULieRehl4t5;CID=tSqTx-fWLck-ncBcWaZNUNo-XhTUc-  
lM0xyHTe1VDmrZzVrMty0u3qSeAw4uTA6eW6Kl

Am I not sending the cookie header properly, because the site is not  
accepting them. Do I need to include more information than just the  
name and the value?

Thanks for your help.

Thank You,  
Ben J.  
E: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 15, 2006, 12:48am UTC](https://www.ruby-forum.com/t/handling-cookies-with-net-http/98488/2 "2006-07-15T00:48:04Z")

</div>

On Sat, Jul 15, 2006 at 07:37:23AM +0900, Ben J. wrote:

> I am having a strange problem and it is partly due to my poor  
> understanding of cookies. So any help is greatly appreciated.

How about using mechanize so you don’t have to worry about handling  
cookies  
and other things like redirects.

[http://www.ntecs.de/blog/Blog/WWW-Mechanize.rdoc](http://www.ntecs.de/blog/Blog/WWW-Mechanize.rdoc)

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 15, 2006, 12:57am UTC](https://www.ruby-forum.com/t/handling-cookies-with-net-http/98488/3 "2006-07-15T00:57:41Z")

</div>

Well, mechanize does a lot I do not need it to. I am looking for as  
much speed as I can get and Net::HTTP seems to be quite a bit faster  
without a lot of the excess features that I do not need. I just need  
some help storing and sending cookies using Net::HTTP.

Thanks.

Thank You,  
Ben J.  
E: removed\_email\_address@domain.invalid
