I wanted to execute one API using the command line but need to pass Cookie: JSESSIONID=<>; auth_cookie=<> value while executing it. We have CAS authentication for the target API.

Please let me know the curl or another command to get Cookie: JSESSIONID=<>; auth_cookie=<> value so I can pass it while executing API.

I have tried with

 curl -v -s --cacert /etc/ssl/certs/cacert.crt https://example.com/login -c cookiefile -d "[email protected]&password=xxxx" -X POST

But it is just storing JSESSIONID in the cookie file. In browser login https://example.com/login will redirect to CAS server login page after authentication it will redirect to https://example.com/index

I followed the approach given by @HBruijn.

ENCODED_DEST=`echo "https://example.com/login/login" | perl -p -e 's/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg' | sed 's/%2E/./g' | sed 's/%0A//g'`
CAS_ID=`curl -L -s --cacert /etc/ssl/certs/cacertga.crt -c $COOKIE_JAR https://cas-server.example2.com/cas/login?service=$ENCODED_DEST | grep name=.lt | sed 's/.*value..//' | sed 's/\".*//'`

This CAS_ID and JSESSIONID need to use in next call

curl -L -s --cacert /etc/ssl/certs/cacertga.crt --data "username=$USERNAME&password=$PASSWORD&lt=$CAS_ID&execution=e1s1&_eventId=submit" -i -b $COOKIE_JAR -c $COOKIE_JAR https://cas-server.example2.com/cas/login?service=$ENCODED_DEST -D .header.txt -o /dev/null

and finally .header.txt should have location URL appending with ticket id. Need to do final call on that location to get auth_cookie. But in my case, it is not giving location URL itself.

I refer solution here

I am editing this question, As I got a solution using python. Use mechanize.Browser() and it will easy to get require auth cookie.

1 Answer 1


The fact that you need to log in first before you can access the API means that a single curl request is probably not sufficient.

You probably need a script with multiple curl requests and work flow similar to:

  1. Make API request with the existing cookies from the cookiefile
  2. Check response code, if OK then the session cookies were not expired yet and you don't need to do more. Yeah!
  3. If the response code indicates an authentication error status (your authentication session is empty or has expired); then you need to log in.
  4. Post your credentials to the CAS server login form.
  5. Check the response code and see if log in was successful and update the cookie file.
    Or die();
  6. GOTO 1.

The standard Chrome developer tools plugin can standard help you to create the basis for such a curl script with the Copy All as cURL option in the network console.

enter image description here

  • Thanks, I tried this approach but still error. I have edited my question with more explanation.
    – Sunil Bhoi
    Aug 23, 2018 at 12:19
  • hey, I used a different approach to this issue. mechanize.Browser() in python that was easy as compared to the shell.
    – Sunil Bhoi
    Mar 13, 2019 at 6:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .