P
Pål Bergström
How can I get the value of a browser cookie with Ruby?
Pål Bergström said:How can I get the value of a browser cookie with Ruby?
Brian said:In what framework? CGI? Rack? Rails? Sinatra? Other?
If you are writing a CGI there is a 'cookies' accessor. For details see
the comments in the source at /usr/lib/ruby/1.8/cgi.rb (or wherever it
is installed on your system)
2010/9/3 P=E5l Bergstr=F6m said:It's rails.
Brian said:In what framework? CGI? Rack? Rails? Sinatra? Other?
Hassan said:And have you looked for 'cookie' in the Rails API docs?
2010/9/3 P=E5l Bergstr=F6m said:Btw, I'm not sure I follow. =A0Does it have to be within a framework?
Isn't the framework built with ruby in the first place? Never mind
Rails. I don't need the cookie there. That's easy. I need it in a script
in /lib.
2010/9/3 P=E5l Bergstr=F6m said:I'm asking about getting a cookie value in Ruby, not Rails.
Pål Bergström said:Hassan Schroeder wrote:
I'm asking about getting a cookie value in Ruby, not Rails.
Jesús Gabriel y Galán said:So, you need to make a request to a site and get the cookie that the
server sends back?
If that's the case then try this:
require 'mechanize'
agent = WWW::Mechanize.new
page = agent.get("http://www.google.com")
p agent.cookies
Looks great and simple. Is Mechanize someting I need to install or is it
part of Ruby?
Hassan said:In short, what is the context?
Jesús Gabriel y Galán said:require 'mechanize'
agent = WWW::Mechanize.new
page = agent.get("http://www.google.com")
p agent.cookies
2010/9/3 P=E5l Bergstr=F6m said:Got it working when installed as a gem. Thank you so much Don't
understand why I didn't get an answer before.
I've asked about this
severeal times. Hopefully I'm on the right track now.
I do get a cookie. A session cookie. But how do I get a particular
cookie?
2010/9/3 Jes=FAs Gabriel y Gal=E1n said:The cookies method returns an array of cookies, you will have to
search for the particular one inside that array: try Array#find method
or such.
Pål Bergström said:It's rails. I used CGI but doesn't seem to work anymore. I just want to
get a value of a cookie with a ruby script in /lib. What about net/http?
When you say you want to "get the value of a browser cookie" it sounds
like you're writing something server-side, to extract the cookies sent
by the browser. But if you're writing a client using net/http or
mechanize, then it's you who *sends* the cookies with each request in
the first place.
Pål Bergström said:I'm sorry if I'm unclear. But I don't understand what the problem is
with my question. So let me ask again.
Before I could get the value in a cookie, any cookie, from a browser
visiting a rails app using cgi. I used it in Rails in order to get a
unique string into the model.
I can't use cookies[:cookie_name] for
that. As I understand it I can't do that with Rails 2.3.x ( or 3?).
The value displayed (which is the value in the cookie) is the timestamp
of the previous request.
So it took a very long time to get here, given that you didn't say in
the first place you were writing a Rails app (probably the question
would have been better directed to a Rails forum then); then in another
posting you said "Never mind
Rails. I don't need the cookie there"; in another you mentioned
net/http.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.