且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在带有 bash 的 CURL 请求中使用变量?

更新时间:2023-02-24 14:57:55

为此使用 jq,正如查尔斯·达菲 (Charles Duffy) 的回答所暗示的那样,这是一个非常好的主意.但是,如果您不能或不想安装 jq,那么您可以使用普通 POSIX shell 执行此操作.

Using jq for this, as Charles Duffy's answer suggests, is a very good idea. However, if you can't or do not want to install jq here is what you can do with plain POSIX shell.

#!/bin/sh
set -e

current_ip="$(curl --silent --show-error --fail ipecho.net/plain)"
echo "IP: $current_ip"

# Update A record
curl -X PUT "https://api.cloudflare.com/client/v4/zones/ZONEIDHERE/dns_records/DNSRECORDHERE" \
    -H "X-Auth-Email: EMAILHERE" \
    -H "X-Auth-Key: AUTHKEYHERE" \
    -H "Content-Type: application/json" \
    --data @- <<END;
{
    "id": "ZONEIDHERE",
    "type": "A",
    "name": "example.com",
    "content": "$current_ip",
    "zone_name": "example.com"
}
END