Jaw. M.
Jaw. M.

Reputation: 149

Save specific part of curl response to variable

I'm currently working on a bash script and I want to save a specific part of a curl-command output to a variable.

This is my curl command with some test parameters: curl -k -I --header "Accept: application/*;version=34.0" --header "Authorization: Basic testXXX1" --request POST https://test.io/api/sessions

The output of the abovementioned curl command:

Date: Mon, 19 Apr 2021 12:13:48 GMT
REQUEST-ID: test1
ACCESS-TOKEN: test1.testXXX.testXX-testXXXX_testXXX_testXXXX-testXX_NR_TESTXX_test_nnndnndnddn_jdjdjdjd_jbjndinfjenkndkn-nnjjnj
TOKEN-TYPE: Bearer
authorization: test1
Content-Type: application/test;version=34.0
REQUEST-EXECUTION-TIME: 12
Cache-Control: no-store, must-revalidate
Vary: Accept-Encoding, User-Agent
Connection: close
Set-Cookie: de.test

Now I just want to catch the value of 'Access-Token' and store it to a variable, so if I echo that variable or want to use it for further commands it'll only return the value of my access token (test1.testXXX.testXX-...).

I'd be glad for any help.

Upvotes: 2

Views: 3705

Answers (1)

wowbagger
wowbagger

Reputation: 630

Multiple options, if you're only after the header:

ACCESS-TOKEN=`curl -qs -k -I --header "Accept: application/*;version=34.0" \
--header "Authorization: Basic testXXX1" \
--request POST https://test.io/api/sessions | grep ACCESS-TOKEN: | sed 's/ACCESS-TOKEN: //g' | tr -d '\r'`

Upvotes: 2

Related Questions