Use curl for Administering Apigee Edge? Here are some tips!

Do you use curl for running administrative tasks on Apigee Edge? Do you use curl in scripts that automate some administrative activities, like importing proxies, exporting proxies, collecting data, provisioning developers, and so on?

If you DON'T, Well why the heck not? It's a super powerful tool that you should get to know. Very helpful for anyone who operates or administers an Apigee Edge system, or uses APIs in other ways. A great tool to add to your toolbox.

If you already DO use curl, Well so do I!! And I've got some tips for you!

  1. Use the .netrc option.

    The curl utility is smart enough to retrieve credentials for a site from the .netrc file (_netrc on Windows). Rather than using the -u username:password form, which exposes your credentials on the terminal command line, you can instruct curl to silently retrieve those data from the .netrc and pass them in the outbound request as a Basic Authorization header.

    Don't:

    curl -u Dchiesa@apigee.com:VerySecret123 https://api.enterprise.apigee.com/.
    	

    Do:

    curl -n https://api.enterprise.apigee.com/.
    	

    To use this, you must have your .netrc file set up in your home directory, with a stanza that looks like this:

    machine api.enterprise.apigee.com 
      login DChiesa@apigee.com
      password VerySecret123
    	

    Keep in mind - you're still passing this data out, so if you employ the -v option on curl, your base64-encoded credentials will still appear on the screen. Or, if you use curl -v within a script, then the credentials will appear in the output. So do be thoughtful. Also, you will want to change the mode on your .netrc file to be 600 or 400, to prevent accidents.

  2. Use the -i option, to check the status code and response headers.

    Curl's -i option prints out the status line and the response headers for your request. This is very helpful when you want to examine what's going on. Sometimes the response content - the body of the response message - is empty. Did the request succeed or not? The only way to know is to look at the status code, and without -i, you can't see the code !

  3. When using curl in a bash script, use the -w , -o , and -s options

    Curl's -w option can be used to print out just the status code! Couple that with the -o option and the -s option, and you have an effective way to grab the output of curl within a script.

    Don't:

    curl -n -X DELETE https://api.enterprise.apigee.com/v1/o/iloveapis2015/vaults/dino-vault/
    	

    Do:

    CURL_RC=`curl -s -n -X DELETE -w "%{http_code}" -o curl_out.txt https://api.enterprise.apigee.com/....`
    	

    After you run that statement, your script can check the CURL_RC variable for the code, and can parse the curl_out.txt for the actual JSON or XML text that comes back.

    Here's an example with more context:

    function MYCURL() {
      local allargs
      local ix=0
      # grab the curl args
      while [ "$1" ]; do
        allargs[$ix]=$1
        let "ix+=1"
        shift
      done
      [ -z "${CURL_OUT}" ] && CURL_OUT=`mktemp /tmp/apigee-${apiname}.curl.out.XXXXXX`
      [ -f "${CURL_OUT}" ] && rm ${CURL_OUT}
      echo "curl ${allargs[@]}"
      # run the curl command
      CURL_RC=`curl -n -s -w "%{http_code}" -o "${CURL_OUT}" "${allargs[@]}"`
      echo "==> ${CURL_RC}"
    }
    ...
      mgmtserver=https://api.enterprise.apigee.com
      org=myorg
      proxyname=proxy1
      MYCURL -X GET "${mgmtserver}/v1/o/$org/apis/$proxyname/deployments"
      if [ ${CURL_RC} -eq 200 ]; then
        echo there are deployments...
        output_parsed=`cat ${CURL_OUT} | grep -A 6 -B 2 "revision"`
        ...
      elif [ ${CURL_RC} -ne 404 ]; then
        echo there are no deployment of that proxy...
      fi
    	

    I've found this pattern to be very valuable in all sorts of scripts I write.

  4. Use curlish!

    curl is super valuable, but I've also found the curlish tool to be a nice ++. It's billed as "curl with flames on top", and that fits! curlish color-formats the response output, making it really easy to interpret visually for those times when I'm using curl from the terminal. Also curl pretty-prints and colorizes JSON output automatically. Super nice.

  5. Don't use curl at all!

    ok, I know this was a list of tips on using curl. But there's a nifty tool I use when there is a repeating set of curl-type tasks I will be doing - and that's Postman, available in the chrome store. Postman allows you to define and save collections of API calls, and to template the input to those calls. Really nice for demonstrations, or when you're showing an audience of people what is possible with a REST call. I can also save a set of API invocation definitions, in a "collection" and share that out. Really handy. Disclaimer: There are many similar tools, and I haven't evaluated most of them. But this one works for me.

ok, those are my tips. Anyone got any others?

Comments
jonesfloyd
Staff

Awesome post, @Dino!

ceber
Staff

One more tip... set an alias!

Here's my simple one for curl:

alias curl='curlish -n'

Now I never forget to use netrc. 😉

And one nitpick. Postman isn't Google's. There's a different crew behind that handy Chrome app:

https://www.getpostman.com/about-us

ozanseymen
Staff

great post @Dino

DChiesa
Staff

Thanks Carlos. Corrected!

Version history
Last update:
‎11-12-2015 05:56 PM
Updated by: