如何使用 shell 脚本 运行 使用 headers 的 curl 命令
How to run a curl command with headers using shell script
我尝试 运行 一个 shell 脚本,其中包含一个 curl 命令及其所需的 headers,如下所示。
counter=1
H1='Content-Type: application/json'
H2='Accept: application/json'
H3='Authorization: Bearer a0a9bb26-bb7d-3645-9679-2cd72e2b4c57'
URL='http://localhost:8280/mbrnd_new/v1/post'
while [ $counter -le 10 ]
do
TEST="curl -X POST --header $H1 --header $H2 --header $H3 -d @s_100mb.xml $URL"
echo $TEST
RESPONSE=`$TEST`
echo $RESPONSE
sleep 5
done
echo "All done"
它给出了一个错误
curl: (6) Could not resolve host: application
curl: (6) Could not resolve host: Bearer
curl: (6) Could not resolve host: a0a9bb26-bb7d-3645-9679-2cd72e2b4c57
<ams:fault xmlns:ams="http://wso2.org/apimanager/security"><ams:code>900902</ams:code><ams:message>Missing Credentials</ams:message><ams:description>Required OAuth credentials not provided. Make sure your API invocation call has a header: "Authorization: Bearer ACCESS_TOKEN"</ams:description></ams:fault>
给定的访问令牌和其他 header 参数正确。当直接调用 'curl' 时它工作正常。
我尝试了不同的方法,比如使用 \" 但没有任何效果。如果有人能对此提供正确的答案,我将不胜感激。
谢谢。
你要执行的命令是这样的:
curl -X POST --header "$H1" --header "$H2" --header "$H3" -d @s_100mb.xml "$URL"
(我将使用 -H
而不是 --header
,因为它更短。)
最简单的方法是
response=$(curl -X POST -H "$H1" -H "$H2" -H "$H3" -d @s_100mb.xml "$URL")
您的解决方案的问题是您根本没有分隔 header 值:
curl -X POST -H $H1
如果H1
的内容是foo: bar
,那么这将扩展为
curl -X POST -H foo bar
这将被解释为
curl -X POST -H (foo:) bar
(使用 (
和 )
只是为了说明优先级,在 shell 中不起作用),即 bar
将被视为第一个位置参数,恰好是主机名,导致您看到奇怪的错误。
你想要的是
curl -X POST -H (foo: bar)
这可以通过将扩展正确地用引号引起来来实现,如上所示。
另外,你应该prefer $(cmd) to `cmd`。
作为最后一条建议,如果您正在学习如何使用 shell,最好避免多次扩展,即不要将您的命令存储在 CMD
变量中到 $($CMD)
之后,因为这会导致在多个地方进行多次扩展(第一个是 CMD
被分配的地方,第二个是 CMD
在 $(...)
sub-shell 中扩展的地方), 让人很难理解到底发生了什么。
我尝试 运行 一个 shell 脚本,其中包含一个 curl 命令及其所需的 headers,如下所示。
counter=1
H1='Content-Type: application/json'
H2='Accept: application/json'
H3='Authorization: Bearer a0a9bb26-bb7d-3645-9679-2cd72e2b4c57'
URL='http://localhost:8280/mbrnd_new/v1/post'
while [ $counter -le 10 ]
do
TEST="curl -X POST --header $H1 --header $H2 --header $H3 -d @s_100mb.xml $URL"
echo $TEST
RESPONSE=`$TEST`
echo $RESPONSE
sleep 5
done
echo "All done"
它给出了一个错误
curl: (6) Could not resolve host: application
curl: (6) Could not resolve host: Bearer
curl: (6) Could not resolve host: a0a9bb26-bb7d-3645-9679-2cd72e2b4c57
<ams:fault xmlns:ams="http://wso2.org/apimanager/security"><ams:code>900902</ams:code><ams:message>Missing Credentials</ams:message><ams:description>Required OAuth credentials not provided. Make sure your API invocation call has a header: "Authorization: Bearer ACCESS_TOKEN"</ams:description></ams:fault>
给定的访问令牌和其他 header 参数正确。当直接调用 'curl' 时它工作正常。
我尝试了不同的方法,比如使用 \" 但没有任何效果。如果有人能对此提供正确的答案,我将不胜感激。
谢谢。
你要执行的命令是这样的:
curl -X POST --header "$H1" --header "$H2" --header "$H3" -d @s_100mb.xml "$URL"
(我将使用 -H
而不是 --header
,因为它更短。)
最简单的方法是
response=$(curl -X POST -H "$H1" -H "$H2" -H "$H3" -d @s_100mb.xml "$URL")
您的解决方案的问题是您根本没有分隔 header 值:
curl -X POST -H $H1
如果H1
的内容是foo: bar
,那么这将扩展为
curl -X POST -H foo bar
这将被解释为
curl -X POST -H (foo:) bar
(使用 (
和 )
只是为了说明优先级,在 shell 中不起作用),即 bar
将被视为第一个位置参数,恰好是主机名,导致您看到奇怪的错误。
你想要的是
curl -X POST -H (foo: bar)
这可以通过将扩展正确地用引号引起来来实现,如上所示。
另外,你应该prefer $(cmd) to `cmd`。
作为最后一条建议,如果您正在学习如何使用 shell,最好避免多次扩展,即不要将您的命令存储在 CMD
变量中到 $($CMD)
之后,因为这会导致在多个地方进行多次扩展(第一个是 CMD
被分配的地方,第二个是 CMD
在 $(...)
sub-shell 中扩展的地方), 让人很难理解到底发生了什么。