Skip to content

Send cookie from one URL to the other using curl in Shell

In this post, we are writing about how to pass cookies from the response of one URL to the request of another URL.

In some cases, when you are testing an application, you might need to pass cookies to various URLs. For example, session details. In curl, user state is not maintained when you hit many URLs. When a user logs in using login URL, its session cookie needs to be stored and then it has to be used for the subsequent requests. To achieve this is a shell script, please read the following steps.

Save cookies of the first URL

Use the –cookie-jar command line option to save the cookies of a request to your local machine.

curl -i --cookie-jar sesscook --data "userName=Test&password=pay&submit=Log In"  http://localhost:8080/TestApp/login.do;

Here I save the session cookie by hitting the login service of my application. The cookie will be saved with the name sesscook.

Pass cookies to the next URL

Use the –cookie option to tell curl to read cookies from the the local path.

curl -i --cookie sesscook  http://localhost:8080/TestApp/next.do;

Full code

curl -i --cookie-jar sesscook --data "userName=Test&password=pay&submit=Log In"  http://localhost:8080/TestApp/login.do;
curl -i --cookie sesscook  http://localhost:8080/TestApp/next.do;
curl -i --cookie sesscook http://localhost:8080/TestApp/logOut.do;

I wanted to test this 500 times to make sure the performances of my services are good. So I put this in a loop.

#!/bin/bash
for i in `seq 1 500`;
    do
    curl -i --cookie-jar sesscook --data "userName=Test&password=pay&submit=Log In"  http://localhost:8080/TestApp/login.do;
    curl -i --cookie sesscook  http://localhost:8080/TestApp/next.do;
    curl -i --cookie sesscook http://localhost:8080/TestApp/logOut.do;
done
See also  Install Apache Server on CentOS 7

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.