bash - CURL inside script is not working as expected for VCD api's -
i trying curl vcloud director api. works fine when tried terminal, fails when tried inside bash script variable substitution.
this script not working:
token=" valid token" url="vcdurl" auth_header=\"x-vcloud-authorization:${token}\" curl -i -k -h "accept:application/*+xml;version=1.5" -h "${auth_header}" -x $url/api/vdc/7f252c90-eb3b-43c4-a5ef-458e5bf22c0e > test.txt echo `cat text.txt` error :http/1.1 400 bad request connection: close
the following works fine:
curl -i -k -h "accept:application/*+xml;version=1.5" -h "x-vcloud-authorization: valid token" -x https://vcdurl/api
spent couple of days figure out stuck here.
token=" valid token" url="https://vcdurl" auth_header=x-vcloud-authorization:${token} curl -i -k -x \ -h "accept:application/*+xml;version=1.5" \ -h "${auth_header}" \ "$url/api/vdc/7f252c90-eb3b-43c4-a5ef-458e5bf22c0e" \ | tee test.txt
note that:
- a leading
https://
has been added url variable's definition, bring parity working manually entered command. - we're quoting expansion of
$url
. (also, note change lower-case; see posix conventions environment variable names -- since shell variables share namespace environment variables, honoring these conventions avoids collisions environment variables having meaning system or shell). - we're no longer injecting literal quotes
auth_header
. (unlike syntactic quotes, parsed shell, literal quotes treated data, , passed throughcurl
). - the (very buggy)
echo $(cat test.txt)
gone. (a sane way writecat test.txt
, way; said,echo "$(cat test.txt)"
remove worst of undesired behaviors;printf '%s\n' "$(<test.txt)"
better, avoiding need either subshell or external utility [ascat
not built shell itself, , has overhead invoke]).
Comments
Post a Comment