views:

156

answers:

1

I'm using the code from the example titled "A Slightly Bigger Example" from this tutorial http://rubylearning.com/blog/2009/09/30/cookie-based-sessions-in-sinatra/ to figure out how to send a cookie to a Sinatra application but I can't figure out how to set the values correctly

When I set the name to be "brandon" in the application it creates a cookie with a value of BAh7BiIJdXNlciIMYnJhbmRvbg%3D%3D%0A which is a url encoding (http://ostermiller.org/calc/encode.html) of the value BAh7BiIJdXNlciIMYnJhbmRvbg==

Using that value I can send a cookie to the app correctly

curl -b "rack.session=BAh7BiIJdXNlciIMYnJhbmRvbg==" localhost:9393

I'm pretty sure that value is a base64 encoding of the ruby hash for the session since the docs (http://rack.rubyforge.org/doc/classes/Rack/Session/Cookie.html) say

The session is a Ruby Hash stored as base64 encoded marshalled data set to :key (default: rack.session).

I thought that meant all I had to do was base64 encode {"user"=>"brandon"} and use it in the curl command. Unfortunately that creates a different value than BAh7BiIJdXNlciIMYnJhbmRvbg==. Next I tried taking the base64 encoded value and decoding it at various base64 decoders online but that results in strange characters (a box symbol and others) so I don't know how to recreate the value to even encode it.

So my question is do you know what characters/format I need to get the proper base64 encoding and/or do you know of another way to pass a value using curl such that it will register as a proper cookie for a Sinatra app?

A: 

f you mean the username and password are entered in a form on a login page, then cURL can "submit" that form like:

curl -d "username=miniape&password=SeCrEt" http://whatever.com/login

and if you want to store the cookie that comes back you do so by specifying a cookie file:

curl -c cookies.txt -d "username=miniape&password=SeCrEt" http://whatever.com/login

and to use those cookie in later requests you do:

curl -b cookies.txt -d "username=miniape&password=SeCrEt" http://whatever.com/login

or do both if you want to both send and receive cookies:

curl -b cookies.txt -c cookies.txt -d "username=miniape&password=SeCrEt" http://whatever.com/login

found this on metafilter

imightbeinatree at Cloudspace
I saw that at metafilter as well but that's for POSTing credentials. I'm trying to specifically pass a cookie along with my GET request to the root handler in the app. Check out the code from the article I linked to at the top of my question here http://gist.github.com/205962#file_second_example.rb
Brandon Toone