# Why doesn't my session work when I run my app via curl

**URL:** <https://www.ruby-forum.com/t/why-doesnt-my-session-work-when-i-run-my-app-via-curl/114303>\
**Category:** Rails\
**Created:** [September 11, 2007, 4:02am UTC](https://www.ruby-forum.com/t/why-doesnt-my-session-work-when-i-run-my-app-via-curl/114303 "2007-09-11T04:02:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 11, 2007, 4:02am UTC](https://www.ruby-forum.com/t/why-doesnt-my-session-work-when-i-run-my-app-via-curl/114303/1 "2007-09-11T04:02:14Z")

</div>

I have my app keep state with a session variable in my DB:

session :session\_key =\> ‘\_myapp\_session\_id’

This works find when I login and run my app via my browser.

But when I login via curl, the session doesn’t seem to work, and once  
I call a action that need authentication, the can’t find my session  
variable. I checked my DB and a row is added everytime I login via  
curl, but the rest of my app can’t seem to read the session variable.

What’s going on? Does the session[] only work via a web browser?

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 11, 2007, 4:55am UTC](https://www.ruby-forum.com/t/why-doesnt-my-session-work-when-i-run-my-app-via-curl/114303/2 "2007-09-11T04:55:20Z")

</div>

eggie5 wrote:

> What’s going on? Does the session[] only work via a web browser?  
> You will most likely need to tell CURL to use the cookie set on login.  
> Google curl + cookie and there are a few explanations on what is  
> required. Alternatively you can try and change you app to not use  
> cookies for tracking sessions (not sure how hard that is in rails).  
> Anyway I think you can get curl to save the cookie to a file and then  
> use that in subsequent requests. But I haven’t done it myself.

Cheers,

Anthony R.

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 11, 2007, 5:49am UTC](https://www.ruby-forum.com/t/why-doesnt-my-session-work-when-i-run-my-app-via-curl/114303/3 "2007-09-11T05:49:25Z")

</div>

Ahhh, thanks alot. I’ve got it working now.

When I login, I need to save my cookie:

eggie5$ curl -d “asfsaf:asfasdf” -c cookie localhost:3000/  
authentication/login

Now, all I need to do for any subsequent API calls is to reference my  
cookie! e.g.:

eggie5$ curl -b cookie localhost:3000/content\_campaigns/5/attachments  
[“5\_1.3g2”, “5\_10.3g2”, “5\_11.3gp”, “5\_12.3gp”, “5\_13.3g2”,  
“5\_14.mp4”, “5\_2.3g2”, “5\_3.3g2”, “5\_4.3g2”, “5\_5.3g2”, “5\_6.3g2”,  
“5\_6.3gp”, “5\_7.3g2”, “5\_8.3g2”, “5\_9.3g2”]

So to save the cookie when you login use curl -c [cookie-file-path]  
this saves the cookie text file to your current directory.  
Then to reference your cookie in subsequent calls use curl -b [cookie-  
file-path]

On Sep 10, 7:14 pm, Anthony R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 11, 2007, 5:10am UTC](https://www.ruby-forum.com/t/why-doesnt-my-session-work-when-i-run-my-app-via-curl/114303/4 "2007-09-11T05:10:21Z")

</div>

My guess is Cookies isn’t supported with curl. Perhap my plugin (link  
below) may be of help.

Cheers,

– Long  
[http://MeandmyCity.com/](http://MeandmyCity.com/) - Find your way  
[http://edgesoft.ca/blog/read/2](http://edgesoft.ca/blog/read/2) - No-Cookie Session Support plugin

----- Original Message -----  
From: “eggie5” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “Ruby on Rails: Talk” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Monday, September 10, 2007 8:59 PM  
Subject: [Rails] why doesn’t my session work when I run my app via curl

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 11, 2007, 7:05am UTC](https://www.ruby-forum.com/t/why-doesnt-my-session-work-when-i-run-my-app-via-curl/114303/5 "2007-09-11T07:05:29Z")

</div>

Yes, thanks for the previous post. I did learn something after all

– Long

----- Original Message -----  
From: “eggie5” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “Ruby on Rails: Talk” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Monday, September 10, 2007 10:49 PM  
Subject: [Rails] Re: why doesn’t my session work when I run my app via  
curl

> > – Longhttp://MeandmyCity.com/- Find your wayhttp://edgesoft.ca/blog/read/2- No-Cookie Session  
> > Support plugin

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [September 11, 2007, 5:51am UTC](https://www.ruby-forum.com/t/why-doesnt-my-session-work-when-i-run-my-app-via-curl/114303/6 "2007-09-11T05:51:26Z")

</div>

No, I figured out how to do it with curl. It’s not too bad. I just  
wonder how valid the use of cookies would be for an API… I’m pretty  
sure any environment would support cookies: C#, java, ruby, flex…
