0

I am using rest-client gem for communication between my CLI client and server. I am using cookies for persisting HTTP session.

@cookies = response.cookies
...
RestClient.post( :cookies => @cookies)

As you can see from the snippet cookies are persistent inside ruby process. I want to persistent them longer than that (as browser does).

Is there native rest-client way for doing that? If not, where should I store them on file system for both *nix and windows systems?

Thanks

Haris Krajina
  • 14,824
  • 12
  • 64
  • 81

2 Answers2

1

One solution is to use RestClient::Resource

Then you could make calls to it, and the cookies would be persisted.

def client(url)
    headers = {
        :accept => :json,
        :content_type => :json,
    }

    cookies = {}
    cookies[SESSION_COOKIE] = @sessionID if @sessionID
    RestClient::Resource.new(url, :headers => headers, :cookies => cookies)
end

To answer your question about where to store them, it depends on if you want them to persist beyond the session. In memory is a perfect place to store them if you only need them short term session.

0

You can use some external storage method like Redis or Memcached to achieve your goal, or simply use a bunch of files to store your cookies (like browsers do).

If you use redis or memcached, you can use domain names as keys. If files, then simply use domain names as file names, and store the cookies into these files. Either way should you serialize the cookie hashes into strings (JSON for example). You can optionally convert the cookie strings into Base64 to prevent shoulder hacking, or encrypt them with RSA for better security in the cost of performance.

Aetherus
  • 8,720
  • 1
  • 22
  • 36