1

I am trying to parse access_token string from the "Set-Cookie" header in the curl POST request executed every 10 seconds and store it in a variable.

This variable will need to be used to execute the GET request, which is every 5 minutes. The variable will be part of the request. The variable is is a access token cookie string that changes everytime the POST request is executed. This access token string will be used in the second curl request for it to run properly.

My question is, how to get only the text after "access_token=" and before the ";" ?

Code

echo $(curl -D text.txt -ivk -H "Content-type: application/json"  --data '{"username":"username", "password":"password", "grant_type":"password" }' https://192.168.0.20/api/v1/auth/login | grep -Fi Set-Cookie)
echo $result

Terminal response

$ /c/Users/mark.fomin/Desktop/test1.sh
> POST /api/v1/auth/login HTTP/1.1
> Host: 192.168.0.20
> User-Agent: curl/7.70.0
> Accept: */*
> Content-type: application/json
> Content-Length: 73
>
} [73 bytes data]
* upload completely sent off: 73 out of 73 bytes
{ [5 bytes data]
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Type: application/json
< X-Powered-By: Plural
< Access-Control-Allow-Origin: *
< Cache-Control: no-store
< Pragma: no-cache
< Set-Cookie: access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJXQS0yMjAwIiwibmJmIjoxNjA3NDUzNDY5LCJleHAiOjE2MDc0NTM3Njl9.KG4GXVLaTQ1TCe2nxOIVjLAHZyGNizbgM0Wb94-dkZI ;refresh_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoicmVmcmVzaF90b2tlbiIsImlzcyI6IldBLTIyMDAiLCJuYmYiOjE2MDc0NTM0NjksImV4cCI6MTYwNzQ1Mzc2OX0.hjJ6Ik5k6mVd19Yb9XVVlojq0_EIL-AGkZnSXmtdGe0; Path=/api; Secure
< Content-Length: 462
< Date: Tue, 08 Dec 2020 18:51:08 GMT
< Server: lighttpd/1.4.45
<
{ [5 bytes data]
100   535  100   462  100    73   1113    175 --:--:-- --:--:-- --:--:--  1286
* Connection #0 to host 192.168.0.20 left intact
Set-Cookie: access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJXQS0yMjAwIiwibmJmIjoxNjA3NDUzNDY5LCJleHAiOjE2MDc0NTM3Njl9.KG4GXVLaTQ1TCe2nxOIVjLAHZyGNizbgM0Wb94-dkZI ;refresh_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoicmVmcmVzaF90b2tlbiIsImlzcyI6IldBLTIyMDAiLCJuYmYiOjE2MDc0NTM0NjksImV4cCI6MTYwNzQ1Mzc2OX0.hjJ6Ik5k6mVd19Yb9XVVlojq0_EIL-AGkZnSXmtdGe0; Path=/api; Secure

2 Answers 2

1

One awk idea that replaces the grep and returns just the desired token:

curl ... | awk -F'[=;]' '/Set-Cookie/{gsub(" ","",$2);print $2;exit}'

Where:

  • -F'[=;]' - use = and ; as input field delimiters
  • /Set-Cookie/ - match any line with the string Set-Cookie (I'm assuming there will only be 1 such line generated by the curl call)
  • dsub(" ","") - remove spaces from field #2
  • print $2 - print field #2 (the desired token)
  • exit - we found what we want so exit

Applying this awk code to the text block (above, under Terminal response) generates:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJXQS0yMjAwIiwibmJmIjoxNjA3NDUzNDY5LCJleHAiOjE2MDc0NTM3Njl9.KG4GXVLaTQ1TCe2nxOIVjLAHZyGNizbgM0Wb94-dkZI

To store in a variable:

$ result=$(curl ... | awk -F'[=;]' '/Set-Cookie/{gsub(" ","",$2);print $2;exit}')
$ echo "${result}"
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJXQS0yMjAwIiwibmJmIjoxNjA3NDUzNDY5LCJleHAiOjE2MDc0NTM3Njl9.KG4GXVLaTQ1TCe2nxOIVjLAHZyGNizbgM0Wb94-dkZI
Sign up to request clarification or add additional context in comments.

Comments

0

Sed alternative:

curl ... | sed -rn '/Set-Cookie/s/(^.*Set-Cookie: access_token=)(.*$)/\2/p'

Look for line in the curl output with "Set Cookie" using sed and then split the line into two sections based on regular expressions. Substitute the line for the second section and print.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.