Search code examples
cookiesgetrest-client

Persist session/cookies across successive get requests


I am running a script that needs to execute several GET requests to an API using the RestClient gem.

However, this API needs cookies & session to be persisted in order to function properly.

I have this:

# first request
url = "http://example-api.com/first-endpoint?param1=foo&param2=bar
request = RestClient.get url, :content_type => :json, :accept => :json
cookies = request.cookies

# second request
url = "http://example-api.com/second-endpoint?param1=foo&param2=bar
request = RestClient.get url, :content_type => :json, :accept => :json

In order to make the second request work, I need to keep the same session and cookies. Using RestClient, I can send back the cookies in a POST request like this:

RestClient.post post_url, {}, {:cookies => cookies}

But I couldn't find in their documentation & on StackOverFlow any way to do this for a GET request.

How should I proceed to "keep" the cookies alive between two successive GET requests ? I'm also open to using another gem or technique if RestClient doesn't support this.

Any help will be much appreciated !


Solution

  • I tried this:

    RestClient.get url,{:cookies => cookies}
    

    And it seems to work fine !