gpt4 book ai didi

linux - 如何使用 cURL 从文件中读取标题?

转载 作者:可可西里 更新时间:2023-11-01 11:46:08 25 4
gpt4 key购买 nike

我找到了 this .

我写了这个变体:

#!/bin/bash
while read line ; do
headers="$headers -H '$line'"
done < public/headers.txt
echo $headers
curl -X PUT \
$headers \
-d @'public/example.json' \
echo.httpkit.com

headers.txt 我有:

X-PAYPAL-SECURITY-USERID:123
X-PAYPAL-SECURITY-PASSWORD:123

但是当我运行 ./public/curl.sh 时,我没有收到我发送的 header 。

我用环境变量隔离了这个问题:

$ x='-H some:asd'
$ curl $x echo.httpkit.com
=> header was NOT present
$ curl -H 'some:asd' echo.httpkit.com
=> header was present
$ curl -H some:asd echo.httpkit.com
=> header was present

如何在 header 部分正确插入变量?

最佳答案

让我们问shellcheck :

In yourscript line 3:
headers="$headers -H '$line'"
^-- SC2089: Quotes/backslashes will be treated literally.
Use an array.

好吧,那我们开始吧

#!/bin/bash
while read line ; do
headers=("${headers[@]}" -H "$line")
done < public/headers.txt
echo "${headers[@]}"
curl -X PUT \
"${headers[@]}" \
-d @'public/example.json' \
echo.httpkit.com

结果:

{
"method": "PUT",
"uri": "/",
"path": {
"name": "/",
"query": "",
"params": {}
},
"headers": {
"host": "echo.httpkit.com",
"user-agent": "curl/7.35.0",
"accept": "*/*",
"x-paypal-security-userid": "123", // <----- Yay!!
"x-paypal-security-password": "123",
"content-length": "32",
"content-type": "application/x-www-form-urlencoded"
},
"body": "\"This is text from example.json\"",
"ip": "127.0.0.1",
"powered-by": "http://httpkit.com",
"docs": "http://httpkit.com/echo"
}

关于linux - 如何使用 cURL 从文件中读取标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24541379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com