user10700023
user10700023

Reputation:

How to pass & correctly escape HTTP headers/arguments to curl in a bash script? Works ok at shell :-/

I run

/usr/local/bin/curl -V
    curl 7.63.0-DEV (x86_64-pc-linux-gnu) libcurl/7.63.0-DEV OpenSSL/1.1.1a zlib/1.2.11 brotli/1.0.3 libidn2/2.0.5 libpsl/0.20.2 (+libidn2/2.0.5) libssh2/1.8.1_DEV nghttp2/1.36.0-DEV

bash --version
    GNU bash, version 4.4.23(1)-release (x86_64-suse-linux-gnu)

This curl query to an api endpoint works perfectly at shell

TOKEN="testtoken" /usr/local/bin/curl \
 -H "content-type: application/json" \
 -H "Authorization: Bearer ${token}" \
-X GET ${url}/endpoint

& returns the expected result, e.g.

{
  "result" : "blah"
}

Only the last line of the command varies. I want to wrap the rest into a bash convenience script.

Trying to avoid escaping-hell, after reading

I'm trying to put a command in a variable, but the complex cases always fail! http://mywiki.wooledge.org/BashFAQ/050

and @ stackoverflow

pass an array of headers to curl into a bash script

I cobbled up this script,

cat test.sh
    #!/bin/bash
    token="testtoken" 

    mk_keyval() {
      local mkey=${1}
      local mval=${2}
      echo "\"${mkey}: ${mval}\""
    }

    mk_hdrs() {
      HDR=()
      HDR[0]=$(mk_keyval "content-type" "application/json")
      HDR[1]=$(mk_keyval "Authorization" "Bearer ${token}")
    }

    mk_hdrs
    echo -e "${HDR[@]/#/-H }\n"
    /usr/local/bin/curl "${HDR[@]/#/-H }" "@"

on exec

bash ./test.sh -X GET ${url}/endpoint

it instead returns

-H "content-type: application/json" -H "Authorization: Bearer testtoken"

curl: (6) Could not resolve host:

The echo'd headers match the at-shell usage, from above,

 -H "content-type: application/json" \
 -H "Authorization: Bearer ${token}" \

How do I get the script's escaping right so it execs the same as the shell command?

Upvotes: 1

Views: 748

Answers (1)

Inian
Inian

Reputation: 85790

I suppose the issue is with the last line where you actually execute the curl command. See

/usr/local/bin/curl "${HDR[@]/#/-H }" "@"
#                                     ^^^ this is an incorrect token, should have been "$@"

The actual positional arguments list in bash is "$@" which should have been used in your command as

/usr/local/bin/curl "${HDR[@]/#/-H }" "$@"

Upvotes: 2

Related Questions