Lee Elson
Lee Elson

Reputation: 1

incorporating cURL output string in BASH variables

I would like to make the process of getting data automatic (I want to have an app that sends/receives data from a car). I'm able to manually do this by copying/pasting results from cURL queries, but I want to put this into a BASH shell script and I don't know how to transfer the results from one cURL command to the next. Here's an example:

result="$(curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"\
        -F "grant_type=password" -F "client_id=$CLIENT_ID" -F "client_secret=$CLIENT_SECRET" -F "email=$EMAIL" -F "password=$PASSWORD"\
        "https://owner-api.teslamotors.com/oauth/token")"

The output from this is:

result:'{"access_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","token_type":"bearer","expires_in":3888000,"refresh_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","created_at":1592850440}'

where XXXXXXXXXXXXXX is a hidden entry. I'm a novice programmer and am unable to take part of the result (e.g. "access_token") and put it in the next cURL:

curl --include --header "Authorization: Bearer {access_token}" "https://owner-api.teslamotors.com/api/1/vehicles"

Is there an easy way to do this with BASH or is it better to use JavaScript or Python? (I'm not very good at any). Other commands return even more complicated strings with 10's of fields. I will want to manipulate these variables (some of which are real numbers, not strings) in the BASH shell. I've tried awk but don't see an easy way to use it.

Upvotes: 0

Views: 205

Answers (3)

markp-fuso
markp-fuso

Reputation: 35266

Assumptions:

  • ${result} will always be a single-line string of characters
  • OP knows the expected format for a given curl result
  • the colon (:) and comma (,) only show up as a field delimiters

Our sample data:

$ result='{"access_token":"OUR_SECRET_ACCESS_TOKEN","token_type":"bearer","expires_in":3888000,"refresh_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","created_at":1592850440}'
$ echo "${result}"
{"access_token":"OUR_SECRET_ACCESS_TOKEN","token_type":"bearer","expires_in":3888000,"refresh_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","created_at":1592850440}

A simple awk solution for a curl result where we want the access_token value:

$ awk -F[:,] '               # define input field separators as colon or comma
{ print $2 }                 # desired token is in field 2
' <<< "${result}"            # feed ${result} as a here-string
"OUR_SECRET_ACCESS_TOKEN"

To capture this in a variable for later use:

$ access_token=$(awk -F[:,] '{ print $2}' <<< "${result}")
$ echo ".${access_token}."   # periods added as visual delimiters for this example
."OUR_SECRET_ACCESS_TOKEN".

This could then be fed to the next curl call like such:

$ curl --include --header "Authorization: Bearer ${access_token}" ...

If we preface our call with set -xv we should see:

$ set -xv                        # turn on debugging
$ curl --include --header "Authorization: Bearer ${access_token}" ...
++ curl --include --header 'Authorization: Bearer "OUR_SECRET_ACCESS_TOKEN"' ...

And if you need to remove the double quotes one option could be:

$ curl --include --header "Authorization: Bearer ${access_token//\"/}" ...
$ ++ curl --include --header 'Authorization: Bearer OUR_SECRET_ACCESS_TOKEN' ...
$ set +xv                        # turn off debugging

If the assumptions aren't always valid (eg, delimiters also show up in the data; result has embedded linefeeds; etc) then you can opt for a more complex awk solution or consider the other answers/suggestions of using a tool (eg, jq) designed for parsing the results from curl.

Upvotes: 0

user2199860
user2199860

Reputation: 818

A program like jq will allow you to parse the json output and access specific attributes from within it.

Upvotes: 0

Michael-O
Michael-O

Reputation: 18415

You should either use jq to process the JSON data, or better yet use Python Requests which makes it very simple for novices to get along. With plain shell and curl you have to do too much manually.

Upvotes: 1

Related Questions