admin管理员组

文章数量:1415145

Probably something answered already elsewhere, but I did not find a proper solution until now.

I want to define a code snippet in yaml for gitlab pipelines containing cURL command that I can reuse in different scripts.

In my below example to call some of our internal API, I am using the EXACT same code in 2 different ways. The full command in curl is working just fine.

curl --location ${OAUTH_URL} --header 'Accept:application/json' --header "x-api-key:${AUTH_CACHING_API_KEY}" --data-urlencode "partition=${PARTITION_ID}" --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode "audience=${OAUTH_AUDIENCE}"

but if I want to use an intermediary variable, and pass it to the curl command, curl does not accept this variable and fails:

CURL_EXTRA_PARAMS=" --header 'Accept:application/json' --header \"x-api-key:${AUTH_CACHING_API_KEY}\" --data-urlencode \"partition=${PARTITION_ID}\""

curl --location ${OAUTH_URL} "${CURL_EXTRA_PARAMS}" --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode "audience=${OAUTH_AUDIENCE}"

Thus the generic question: how to pass a string variable containing multiple parameters into a curl command ?

Probably something answered already elsewhere, but I did not find a proper solution until now.

I want to define a code snippet in yaml for gitlab pipelines containing cURL command that I can reuse in different scripts.

In my below example to call some of our internal API, I am using the EXACT same code in 2 different ways. The full command in curl is working just fine.

curl --location ${OAUTH_URL} --header 'Accept:application/json' --header "x-api-key:${AUTH_CACHING_API_KEY}" --data-urlencode "partition=${PARTITION_ID}" --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode "audience=${OAUTH_AUDIENCE}"

but if I want to use an intermediary variable, and pass it to the curl command, curl does not accept this variable and fails:

CURL_EXTRA_PARAMS=" --header 'Accept:application/json' --header \"x-api-key:${AUTH_CACHING_API_KEY}\" --data-urlencode \"partition=${PARTITION_ID}\""

curl --location ${OAUTH_URL} "${CURL_EXTRA_PARAMS}" --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode "audience=${OAUTH_AUDIENCE}"

Thus the generic question: how to pass a string variable containing multiple parameters into a curl command ?

Share Improve this question edited Feb 11 at 17:48 EricBDev asked Feb 11 at 17:16 EricBDevEricBDev 1,60318 silver badges22 bronze badges 2
  • 1) 'not working' is not sufficient on a professional Q/A website 2) use an MCVE to allow us to test 3) use debug/add errors – Gilles Quénot Commented Feb 11 at 17:21
  • My question is very generic, and my code snippet above is just to show I want to do the EXACT same code in 2 different ways. I rephrased my question. – EricBDev Commented Feb 11 at 17:49
Add a comment  | 

2 Answers 2

Reset to default 1

It seems no one understood my question properly, despite my rephrasing attempt: my bad, not able to make it clear enough.

But I found the solution myself.

I already echoed the full command to ensure it was giving exactly the same OUTPUT than the working variant before asking the question. Did not help since echo is cleaning the escape characters.

However, using the -v (verbose) option in curl confirmed what I suspected: my issue was a pure string escape issue.

Eventually, removing all internal single and double quotes from the intermediary variable did the job!

CURL_EXTRA_PARAMS="--header Accept:application/json --header x-api-key:${AUTH_CACHING_API_KEY} --data-urlencode partition=${PARTITION_ID}"

is propagated properly into the command

curl --location ${OAUTH_URL} ${CURL_EXTRA_PARAMS} --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode "audience=${OAUTH_AUDIENCE}"

curl --location ${OAUTH_URL} ${CURL_EXTRA_PARAMS}

Please test with echo $CURL_EXTRA_PARAMS in the script to see what the contents of the var are

本文标签: shelluse curl with variable containing headersStack Overflow