benqzq
By:
benqzq

Working with long DIgitalOcean API commands

February 1, 2018 139 views
API Ubuntu 16.04

I have this very long and heavy DigitalOcean API command to rebuild droplet (I have a mistake there which I'm trying to figure out but it's just for example to longevity problem).

I think it would be better of to work with multi-lined, because in one long row it's just extremely uncomfortable to maintain. For example, look how long this one liner is:

curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer 81206074d17245ba5d106d788b9edfea9947b1712ab47efgd6888e67f2e79957" -d '{"type":"rebuild","image":"ubuntu-16-04-x64"}' "https://api.digitalocean.com/v2/droplets/3164450/actions" 

Maybe it's best to do:

curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer 81206074d17245ba5d106d788b9edfea9947b1712ab47efgd6888e67f2e79957" \
 -d '{"type":"rebuild","image":"ubuntu-16-04-x64"}' \
"https://api.digitalocean.com/v2/droplets/3164450/actions" 

Maybe something extra (a bash function? a Bash script?)

Please share any tip, thanks!


P.S. I've already deleted that token, I just gave it for the example here.

1 Answer
asb MOD February 1, 2018
Accepted Answer

Breaking it up into multiple lines definitely makes it more readable and easier to understand. You might also be interested in looking at the doctl command line tool. The same command using it would be:

doctl compute droplet-action rebuild 3164450 --image ubuntu-16-04-x64

If you're are using it in a script, you can also pass --output json in order to have the same JSON response you would receive using cURL.

DigitalOcean's web based control panel provides a convenient, point-and-click interface for managing Droplets. There are many times, however, when a command-line tool may be a preferable alternative. doctl, the official DigitalOcean command-line client, leverages the DigitalOcean API to provide access to most account and Droplet features.
Have another answer? Share your knowledge.