想知道为什么尝试使用cURL将Letsencrypt .pem证书上传到Cloudflare API会引发“请求正文中格式错误的JSON”错误?

时间:2018-12-13 11:08:19

标签: api ssl cloudflare lets-encrypt certbot

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

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}

尝试使用引号建议作为对以下问题的回答,但仍然是相同的错误。

Why do I get a malformed JSON in request body in this cURL call?

1 个答案:

答案 0 :(得分:0)

我广泛搜索了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”,$ 0;}'来实现此目的,但现在它引发了“无效证书”响应。

以下代码可以正常工作。上面的awk命令将换行符替换为新行\ n,但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",$0;}' $PRIVATE_KEY`
CERTIFICATE=`awk 'NF {sub(/\r/, ""); printf "%s\\\n",$0;}' $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