Khalfe
Khalfe

Reputation: 15

extract token from curl result by shell script

I write this script

#!/bin/bash
# cm.sh

  curl -i \
  -H "Content-Type: application/json" \
  -d '
{ "auth": {
    "identity": {
      "methods": ["password"],
      "password": {
        "user": {
          "name": "admin",
          "domain": { "id": "default" },
          "password": "secret"
        }
      }
    }
  }
}' \
  "http://localhost/identity/v3/auth/tokens" ; echo



echo $tokenizer1 
echo $tokenizer2

But all of them(awk or sed) it's the same

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   540  100   312  100   228    312    228  0:00:01 --:--:--  0:00:01  5142

My goal is to put the token in a variable for later. Thanks guys in advance.

Upvotes: 1

Views: 7777

Answers (2)

Christophe
Christophe

Reputation: 428

Instead of using the direct result of cURL, you could save the result in a file, and use your grep command on it. Something like this maybe :

curl -o boulou.txt http://localhost/identity/v3/auth/tokens && cat boulou.txt | grep "X-Subject-Token" | awk '{printf $2}'

Edit, if you just want you desired output, add the --silent to the cURL command :

curl -o boulou.txt http://localhost/identity/v3/auth/tokens --silent && cat boulou.txt | grep "X-Subject-Token" | awk '{printf $2}'

Edit 2: If you want to export it, and delete your file, you could use something like this :

export OS_TOKEN=$(curl -o billy.txt hhttp://localhost/identity/v3/auth/tokens --silent &&  cat billy.txt | grep "X-Subject-Token" | awk '{printf $2}') && rm billy.txt

Upvotes: 2

that other guy
that other guy

Reputation: 123570

"How do I use grep/awk to extract a header in a field from curl when I pass it a JSON document when the contents is stored in a variable?" is a very tricky and unique problem indeed.

However, if you gradually mock out every part of your code to narrow it down, you'll discover that this is the much easier question you could have researched or asked instead:

How do I use grep/awk on contents from a variable?

I have a variable containing HTTP headers, and I want to extract the value of one of them. Here's an example:

variable='Foo: 1
Bar: 2
Baz: 3'

This is what I've tried to get 2 from Bar:

  # Just hangs
  tokenizer1=$variable `grep "Bar" | awk '{printf $2}'`
  # Is empty
  tokenizer2=$variable | `grep "Bar" | awk '{printf $2}'`

The answer here is to use echo to pipe the contents so that grep can read it on stdin:

tokenizer3=$(echo "$variable" | grep "Bar" | awk '{printf $2}')

This is easily applied to your example:

tokenizer3=$(echo "$token" | grep "X-Subject-Token" | awk '{printf $2}')
echo "The value is $tokenizer3"

Upvotes: 0

Related Questions