小编Ale*_*x S的帖子

如何在 curl 命令中将标头作为参数传递?

我正在尝试执行使用预定义变量作为标头的 curl 命令。

header='-H "Content-Type: application/json" -H "userGUID: 7feb6e62-35da-4def-88e9-376e788ffd97" -H "Content-Length: 51"'
Run Code Online (Sandbox Code Playgroud)

这本质上是 curl 命令

curl $header -w "http code: %{http_code}\\n" -X POST -d $BODY $URL 
Run Code Online (Sandbox Code Playgroud)

然后返回错误消息

rl: (6) Could not resolve host: application
curl: (6) Could not resolve host: 7feb6e62-35da-4def-88e9-376e788ffd97"
curl: (6) Could not resolve host: 51"
Run Code Online (Sandbox Code Playgroud)

这按预期工作

curl -H "Content-Type: application/json" -H "userGUID: 7feb6e62-35da-4def-88e9-376e788ffd97" -H "Content-Length: 51" -w "http code: %{http_code}\\n" -X POST -d $BODY $URL 
Run Code Online (Sandbox Code Playgroud)

我试图将标头作为变量传递的原因是因为我正在编写一个循环遍历数组的脚本,但目前由于某种原因这不适用于标头。为 body 传递参数没有问题。

bash shell curl

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

bash ×1

curl ×1

shell ×1