想知道为什么尝试使用 cURL 将 Letsencrypt .pem 证书上传到 Cloudflare API 会抛出 "Malformed JSON in request body" 错误?

Wonder why trying to upload a Letsencrypt .pem certificate using cURL to Cloudflare API throws "Malformed JSON in request body" error?

我有一个脚本试图替换我网站在 Cloudflare 上的 SSL 证书。 Certbot 每三个月从 Letsencrypt 自动更新本地服务器上的证书。现在,需要使用 certbot 更新的 SSL .pem 证书上传到 Cloudflare API。

PRIVATE_KEY="/etc/letsencrypt/live/autoxxx.com.au/privkey.pem"

CERTIFICATE="/etc/letsencrypt/live/aautoxxx.com.au/cert.pem"

# read from files
PRIVATE_KEY=`cat $PRIVATE_KEY`
CERTIFICATE=`cat $CERTIFICATE`

DATA='{"private_key":"'$PRIVATE_KEY'","certificate":"'$CERTIFICATE'","bundle_method":"ubiquitous"}' 

curl -i \
     -X PATCH "https://api.cloudflare.com/client/v4/zones/rCWR4i3A24NZEzI4dFLYLAhU7tUBtJUSYQkh/custom_certificates/iqXVG2FV8Cgj5FXGMexIoJovtFQx5UhecVya" \
     -H "X-Auth-Email: webdev@autoxxx.com.au" \
     -H "X-Auth-Key: pg5Q89JI33nsgdA9iZwPky3q" \
     -H "Content-Type: application/json" \
     -d "$DATA" --trace-ascii /dev/stdout

但是,运行这个脚本抛出以下错误

{"success":false,"errors":[{"code":6007,"message":"Malformed JSON in request body"}],"messages":[],"result":null}

尝试了回答以下问题的引号建议,但仍然是同样的错误。

我广泛搜索了 Google。 尝试了以下 https://docs.vmware.com/en/Unified-Access-Gateway/3.0/com.vmware.access-point-30-deploy-config.doc/GUID-870AF51F-AB37-4D6C-B9F5-4BFEB18F11E9.html 将 .pem 放入一行。使用 awk 'NF {sub(/\r/, ""); printf "%s\n",[=17=];}' 来实现这一点,但现在它抛出了 "Invalid Certificate" 响应。

以下代码完美运行。上面的 awk 命令用新行 \n 替换回车符 returns,但 Cloudflare 显然需要文字“\n”。

PRIVATE_KEY="/etc/letsencrypt/live/autoxxx.com.au/privkey.pem"

CERTIFICATE="/etc/letsencrypt/live/aautoxxx.com.au/cert.pem"

# read from file, put the .pem into single line and replace carriage returns with the literal "\n"
PRIVATE_KEY=`awk 'NF {sub(/\r/, ""); printf "%s\\n",[=10=];}' $PRIVATE_KEY`
CERTIFICATE=`awk 'NF {sub(/\r/, ""); printf "%s\\n",[=10=];}' $CERTIFICATE`

DATA='{"private_key":"'$PRIVATE_KEY'","certificate":"'$CERTIFICATE'","bundle_method":"ubiquitous"}' 

curl -i \
     -X PATCH "https://api.cloudflare.com/client/v4/zones/rCWR4i3A24NZEzI4dFLYLAhU7tUBtJUSYQkh/custom_certificates/iqXVG2FV8Cgj5FXGMexIoJovtFQx5UhecVya" \
     -H "X-Auth-Email: webdev@autoxxx.com.au" \
     -H "X-Auth-Key: pg5Q89JI33nsgdA9iZwPky3q" \
     -H "Content-Type: application/json" \
     -d "$DATA" --trace-ascii /dev/stdout