Search code examples
bashapishellcurlsh

Passing key and pem file data in curl command not working


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?


Solution

  • 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.