Search code examples
bashcurlcronubuntu-16.04ubuntu-server

Nested cURL call


I've one cron task to update my DDNS with my current ip address and do it through a cURL call.

The problem is one of the parameters to pass in the call is the CURRENT IP and in order to discover ir i need to do another cURL call.

I would like to know if is possible to nest two cURL calls in one single script in order to make my cron task avoiding extra scripts

example:

to get my current ip I use

curl ipinfo.io/ip

to update my ddns i need to do:

curl -X PUT "https://api.cloudflare.com/client/v4/zones/2wertyh/dns_records/23ertghj" \
     -H "X-Auth-Email: tomatechines@gmail.com" \
     -H "X-Auth-Key: 123ertgyh" \
     -H "Content-Type: application/json" \
     --data '{"type":"A","name":"qwsdfg.com.br","content":"MY-CURRENT-IP","ttl":1800,"proxied":false}'

how can i fit this two calls together in order to make my cron task


Solution

  • Use command substitution, like this:

    curl -X PUT "https://api.cloudflare.com/client/v4/zones/2wertyh/dns_records/23ertghj" \
         -H "X-Auth-Email: tomatechines@gmail.com" \
         -H "X-Auth-Key: 123ertgyh" \
         -H "Content-Type: application/json" \
         --data '{"type":"A","name":"qwsdfg.com.br","content":"'"$(curl ipinfo.io/ip)"'","ttl":1800,"proxied":false}'
    

    String argument for --data is composed from three concatenated parts, 'beginning' "$(curl ...)" 'ending' (more details see in this answer).