# How to handle cookie with ruby

**URL:** https://rubytalk.org/t/how-to-handle-cookie-with-ruby/41049
**Category:** ruby-talk
**Created:** [30 September 2007 08:17 UTC](https://rubytalk.org/t/how-to-handle-cookie-with-ruby/41049 "2007-09-30T08:17:24Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![leon\_du](https://avatars.discourse-cdn.com/v4/letter/l/57b2e6/32.png) [@leon\_du](https://rubytalk.org/u/leon_du)
#### Post date: [30 September 2007 08:17 UTC](https://rubytalk.org/t/how-to-handle-cookie-with-ruby/41049/1 "2007-09-30T08:17:24Z")

</div>

Hi all, I dont know how to handle cookie with ruby.  
the problem is I want to fetch content from a website which need login  
first.  
I use the http.post to do the login and seemed works well, but when I went  
to the index page, still I got the login first message.  
so, any suggestions?

---------------------below is my code---------------------------  
require 'net/http'  
require 'cgi'  
require 'iconv'

def fetch\_content  
&nbsp;&nbsp;Net::HTTP.start ('domain' ) do |server|  
&nbsp;&nbsp;&nbsp;&nbsp;id = CGI.escape("id" )  
&nbsp;&nbsp;&nbsp;&nbsp;passwd = CGI.escape("pw" )  
&nbsp;&nbsp;&nbsp;&nbsp;params = "id=#{id}&pwpwd=#{passwd}"  
&nbsp;&nbsp;&nbsp;&nbsp;resp = server.post2('/login.php?' , params,headers)

&nbsp;&nbsp;&nbsp;&nbsp;# I got a 200 OK, the resp.body shows that the login is successful

&nbsp;&nbsp;&nbsp;&nbsp;params = ""  
&nbsp;&nbsp;&nbsp;&nbsp;resp = server.post2('/index.php' , params,headers)  
&nbsp;&nbsp;&nbsp;&nbsp;# after checking the resp.body , the index page still says I need to  
login  
&nbsp;&nbsp;end  
end

---

<div class="post-metadata">

### Author: ![mortee](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@mortee](https://rubytalk.org/u/mortee)
#### Post date: [30 September 2007 12:10 UTC](https://rubytalk.org/t/how-to-handle-cookie-with-ruby/41049/2 "2007-09-30T12:10:44Z")

</div>

leon du wrote:

> Hi all, I dont know how to handle cookie with ruby.  
> the problem is I want to fetch content from a website which need login  
> first.  
> I use the http.post to do the login and seemed works well, but when I went  
> to the index page, still I got the login first message.  
> so, any suggestions?

I guess you should try e.g. Mechanize:

[http://mechanize.rubyforge.org/mechanize/](http://mechanize.rubyforge.org/mechanize/)

which is designed for just this kind of thing.

---

<div class="post-metadata">

### Author: ![leon\_du](https://avatars.discourse-cdn.com/v4/letter/l/57b2e6/32.png) [@leon\_du](https://rubytalk.org/u/leon_du)
#### Post date: [1 October 2007 03:50 UTC](https://rubytalk.org/t/how-to-handle-cookie-with-ruby/41049/3 "2007-10-01T03:50:44Z")

</div>

thanks a lot, mechanize seemed to be the solution 🙂

> **···**
>
> 2007/9/30, mortee \<mortee.lists@kavemalna.hu\>:
> 
> > leon du wrote:  
> > \> Hi all, I dont know how to handle cookie with ruby.  
> > \> the problem is I want to fetch content from a website which need login  
> > \> first.  
> > \> I use the http.post to do the login and seemed works well, but when I  
> > went  
> > \> to the index page, still I got the login first message.  
> > \> so, any suggestions?
> > 
> > I guess you should try e.g. Mechanize:
> > 
> > [http://mechanize.rubyforge.org/mechanize/](http://mechanize.rubyforge.org/mechanize/)
> > 
> > which is designed for just this kind of thing.
