6

I've started picking up bash scripting and I'm stuck at something I'm trying to wrap my head around.

I have a curl command that outputs a token and I need to use it in the following command:

curl -k 'https://server:port/session' -X POST -H 'Content-Type: application/json' -d '{"username":"admin","password":"password"}'

It then outputs a token here:

{"token":"ac07098ad59ca6f3fccea0e2a2f6cb080df55c9a52fc9d65"}

I then need to use it in the follow up command

curl https://server:port/ -k -X POST -H 'Content-Type: application/json' -H 'X-Cookie:token=token' -d '

I was thinking I could output the token to a file, then have a sed command write the token to a file, then the new command use a variable where token=$token

Thanks!

4 Answers 4

13

This is where a JSON parsing tool comes in handy (such as ):

$ echo '{"token":"ac07098ad59ca6f3fccea0e2a2f6cb080df55c9a52fc9d65"}' | jq -r .token
ac07098ad59ca6f3fccea0e2a2f6cb080df55c9a52fc9d65

So

json=$( curl -k 'https://server:port/session' -X POST -H 'Content-Type: application/json' -d '{"username":"admin","password":"password"}' )
token=$( jq -r ".token" <<<"$json" )

curl https://server:port/ -k -X POST -H "X-Cookie:token=$token" ...
Sign up to request clarification or add additional context in comments.

1 Comment

Install WSL and have Linux on Windows.
10

With no further tool than a bash (tested Centos/Rhel7/GitBash) :

json=$(curl -k 'https://server:port/session' \
            -X POST -H 'Content-Type: application/json' \
            -d '{"username":"admin","password":"password"}') \
&& token=$(echo $json | sed "s/{.*\"token\":\"\([^\"]*\).*}/\1/g") \
&& echo "token = $token"

then use your authentication needing commands like that :

curl https://server:port/ -k -X POST \
                          -H 'Content-Type: application/json' \
                          -H 'X-Cookie:token=$token' -d ...'

2 Comments

Hi @teikitel What if I am using Bearer Token in format Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ5bWFudWNoYXJpYW4iLCJleHAiOjE2MjI3OTc5Mzl9.d7yT6r8pf0QEh-sMDxNiObDSdF2K17l3WqcjHnv9800
Hello @MichaelKors, usually if the case your authentication service retruns a reponse payload containing only a string like "Bearer eyJhbG...". So it's even simpler, no need to parse it: token=$json. Then the http header you should use is -H 'Authorization: $token' instead of -H 'X-Cookie...'
1

If Python is installed, and hopefully it is on modern systems, you can do something like:

OUTPUT="$(curl -k 'https://server:port/session' -X POST -H 'Content-Type: application/json' -d '{"username":"admin","password":"password"}' | python -c "import sys, json; print json.load(sys.stdin)['token']")"

This will give you:

echo $OUTPUT
ec2e99a1d294fd4bc0a04da852ecbdeed3b55671c08cc09f

Comments

-2

Use the `` syntax:

cmd1result=$(command1 | cut -d ':' -f 2 | grep -Po "[a-z0-9-]+")
command2 $cmd1result

4 Comments

it works, however, I just need the "ac07098ad59ca6f3fccea0e2a2f6cb080df55c9a52fc9d65" portion without quotes. It'll always be a random 48 character string. Example: 'X-Cookie:token=ac07098ad59ca6f3fccea0e2a2f6cb080df55c9a52fc9d65'
$() is the modern replacement. Backticks are literally legacy support, and have serious disadvantages -- it's easy to nest $( $( ) ), whereas putting backticks inside backticks is painful.
And your grep command strips hex digits from the token. It's thus inaccurate/corrupt, and (due to unnecessary use of the nonstandard option -P) needlessly platform-specific.
(note that the e in the word token is part of the character set that needs to be preserved, since e is within [0-9a-f] -- so just stripping a character range is quite thoroughly unworkable).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.