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
2 Answers
Reset to default 1It 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
版权声明:本文标题:shell - use curl with variable containing headers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745208514a2647753.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论