0

I am trying to convert a one line REST API command into a script. I have used various tips I have on this forum but am still unsuccessful. My one line script looks like this

curl -F file=@data-file.csv https://rest-api-host-dev/v1/schools/351/roster  -H "Authorization: Bearer $( curl -d "@creds.json" -w "\n" -H "Content-Type: application/json" -X POST https://rest-api-host/v1/auth/login | grep -Po '"access_token":.*?[^\\]",' | awk -F\" '{print $4}')"

Works just fine. I have tried to break it into variable subsitution for portability. That's when the issues happen

AWK=/bin/awk
CURL=/usr/bin/curl
GREP=/bin/grep
ROSTERFILE=data-file.csv

# Production= 355
# Staging= 351
INSTITUTIONID=351
URI="/v1/schools/${INSTITUTIONID}/roster"

# Production= " https://rest-api-host-prod"
# Staging= " https://rest-api-host-dev"
URL=" https://rest-api-host-dev"

GETTOKEN="${CURL} -d \"@creds.json\"  -w \"\n\" -H \"Content-Type: application/json\" -X POST ${URL}/v1/auth/login"
GREPTOKEN="${GREP} -Po '\"access_token\":.*?[^\\\\]\",'"
PRINTTOKEN="${AWK} -F\\\" '{print \$4}'"


# Token is obtained from curl based authentication
TOKEN=`${GETTOKEN} | ${GREPTOKEN} | ${PRINTTOKEN}`

UPLOADARGS=' -H "Authorization: Bearer ${TOKEN}"'

#
#*******************************************************
# Main Program
#*******************************************************
#

${CURL} -F file=@${ROSTERFILE} ${URL}${URI} ${UPLOADARGS}

I have tried escaping single and double quotes I have used `` and $() to place in variables. Still get errors

example test
+ AWK=/bin/awk
+ CURL=/usr/bin/curl
+ GREP=/bin/grep
+ ROSTERFILE=data-file.csv
+ INSTITUTIONID=351
+ URI=/v1/schools/351/roster
+ URL=' https://rest-api-host-dev'
+ GETTOKEN='/usr/bin/curl -d "@creds.json"  -w "\n" -H "Content-Type: application/json" -X POST https://rest-api-host-dev/v1/auth/login'
./restapi-test.sh: line 52: unexpected EOF while looking for matching `"'
./restapi-test.sh: line 62: syntax error: unexpected end of file

+ AWK=/bin/awk
+ CURL=/usr/bin/curl
+ GREP=/bin/grep
+ ROSTERFILE=data-file.csv
+ INSTITUTIONID=351
+ URI=/v1/schools/351/roster
+ URL=' https://rest-api-host-dev'
+ GETTOKEN='/usr/bin/curl -d "@creds.json"  -w "\n" -H "Content-Type: application/json" -X POST  https://rest-api-host-dev/v1/auth/login'
+ GREPTOKEN='/bin/grep -Po '\''"access_token":.*?[^\\]",'\'''
+ PRINTTOKEN='/bin/awk -F\" '\''{print $4}'\'''
++ /usr/bin/curl -d '"@creds.json"' -w '"\n"' -H '"Content-Type:' 'application/json"' -X POST https://rest-api-host-dev/v1/auth/login
++ /bin/grep -Po ''\''"access_token":.*?[^\\]",'\'''
++ /bin/awk '-F\"' ''\''{print' '$4}'\'''
awk: '{print
awk: ^ invalid char ''' in expression

curl: (6) Couldn't resolve host 'application'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0    13      0     72 --:--:-- --:--:-- --:--:--     0
+ TOKEN=

Any help is appreciated.

-Chris

Chris
  • 3
  • 2
  • 2
    Save yourself the trouble and use a function instead of trying to stick a command in a string – that other guy Feb 27 '19 at 23:09
  • 3
    Quoting and escaping any non-trivial command as a string variable is more trouble than it's worth. At the very least, store the command arguments in an array and execute the command with`somecmd "${args[@]}"`. You might want to read this: [Bash FAQ](https://mywiki.wooledge.org/BashFAQ/050). – Mike Holt Feb 27 '19 at 23:17
  • 1
    [this question](https://stackoverflow.com/questions/13365553/setting-an-argument-with-bash) shows how to put the arguments in an array. But the suggestion to use a function is a much better idea. – Barmar Feb 28 '19 at 01:10
  • Don't use `grep` or `awk` to parse a JSON response; get a proper JSON parser like `jq` instead. – chepner Feb 28 '19 at 04:14

0 Answers0