传递密钥和.pem文件数据在curl命令中不起作用。

huangapple go评论63阅读模式
英文:

Passing key and pem file data in curl command not working

问题

我正在尝试使用curl命令调用API,但不幸的是,我无法保留文件。我正在尝试在命令中传递.key.pem文件的数据,但我无法正确传递。以下是我的.sh文件中的命令:

response=$(curl --key "$5" --cert "$6" -k -X "$2" -d "$payload" "$4")

我以以下方式调用脚本:

key="${key}"
pem="${pem}"

bash ./Integration1.sh Provision POST "$payload" https://some-api.com/pr "$key" "$pem"

它给出以下错误:

curl: (58) could not load PEM client certificate from -----BEGIN CERTIFICATE-----

如果我直接传递文件,此命令可以正常工作,所以是否有办法通过字符串变量在curl命令中传递数据?

英文:

I am trying to invoke the API using the curl command but unfortunately, I won't be able to keep the files. I am trying to pass the .key and .pem file's data in the command but I am not able to pass that correctly. Below is my command in my .sh file:

response=$(curl --key "$5" --cert "$6" -k -X "$2" -d "$payload" "$4")   

I am calling the script below way:

key="${key}"
pem="${pem}"

bash ./Integration1.sh Provision POST "$payload" https://some-api.com/pr "$key" "$pem"   

It gives the below error:

curl: (58) could not load PEM client certificate from -----BEGIN CERTIFICATE-----

This command works fine if I pass the file directly so, is there any way to pass the data via string variables in the curl command?

答案1

得分: 2

如果您只有将密钥数据存储在一个变量中,由于某种原因无法将其自己写入文件,另一种解决方案是使用进程替代

<(printf '%s' "$key") \
<(printf '%s' "$pem")

这需要使用bash,并在底层仍然使用文件,但它不需要您自己管理文件或知道它们的位置。

英文:

If you only have your key data in a variable and can't write it to a file yourself for some reason, an alternative solution is to use process substitution.

bash ./Integration1.sh Provision POST "$payload" https://some-api.com/pr \
<(printf '%s' "$key") \
<(printf '%s' "$pem")

This requires bash and still uses files under the hood, but it doesn't require you to manage the files yourself or know where they're located.

答案2

得分: 1

--key--cert 需要输入包含证书数据的文件的名称,而不是证书数据本身。

... "$(cat my_client.key)" "$(cat my_client.pem)"

应该改为

... my_client.key my_client.pem
英文:

--key and --cert take the name of a file containing certificate data, not the certificate data itself.

... "$(cat my_client.key)" "$(cat my_client.pem)"

Should just be

... my_client.key my_client.pem

huangapple
  • 本文由 发表于 2023年2月16日 01:59:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定