Blog

Explore Akamai APIs with HTTPie v2

January 28, 2020 · by Javier Garza

In case you don’t already know, HTTPie is a great command-line tool for debugging websites and exploring APIs. 

It supports similar features to command-line tools like cURL or Wget, but with a flexible syntax that makes it much easier to add headers, query-strings, or request payloads - and it supports JSON.

Akamai provides an authentication extension for HTTPie that makes it ideal to explore Akamai APIs that don’t have a CLI package. 

On January 15, 2020, HTTPie v2 was released (over a year after the release of v1).

Below, we’ve listed some of the improvements you can expect from v2.

  • Added the following commands: 
    • --offline: allows you to build an HTTP request and print it without actually sending anything over the network
    • --compress: allows you to compress the request body
    • --ignore-netrc: allows you to bypass credentials from .netrc
    • --max-headers: allows you to set the max header limit
  • Added an https alias command with https:// as the default scheme
  • Added $ALL_PROXY documentation
  • Improved the --debug output formatting
  • Removed Python 2.7 support (EOL Jan 2020) 
  • Removed Python’s default limit of 100 response headers
  • Removed the 30-second connection --timeout limit

View the HTTPie changelog to see all of the latest improvements

Below, you’ll see a few sample requests that walk through how you can use HTTPie with the Akamai Edgegrid plugin to run a few API calls (the example below shows 3 of resources available in the Diagnostics API):

Example 1: Get a list of locations

This example shows how you can get a list of locations in which you can run diagnostic calls and filter by location (ex: Spain) by using jq and grep.

$ http --auth-type edgegrid --auth devrel-admin: :/diagnostic-tools/v2/ghost-locations/available  | jq .locations | grep -i spain
   "id": "barcelona-spain",
    "value": "Barcelona, Spain"
    "id": "madrid-spain",
    "value": "Madrid, Spain"

Example 2: Post IP addresses 

This example shows you how to launch a cURL request from a server in Madrid, Spain against the URL: http://whatismyip.akamai.com (which returns the IP).

You can see the returned IP address in the responseBody field.

$ http --auth-type edgegrid --auth devrel-admin: --print=b POST :/diagnostic-tools/v2/ghost-locations/madrid-spain/curl-results url="http://whatismyip.akamai.com"


{
    "curlResults": {
        "httpStatusCode": 200,
        "responseBody": "84.53.132.244\nEXIT: 0 stripnl\n",
        "responseHeaders": {
            "Cache-Control": "max-age=0, no-cache, no-store",
            "Connection": "keep-alive",
            "Content-Length": "13",
            "Content-Type": "text/html",
            "Date": "Wed, 22 Jan 2020 04:00:46 GMT",
            "Expires": "Wed, 22 Jan 2020 04:00:46 GMT",
            "Pragma": "no-cache"
        }
    }
}

Example 3: Print details 

This example shows how you can check if an IP address is an Akamai server.

$ http --auth-type edgegrid --auth devrel-admin: :/diagnostic-tools/v2/ip-addresses/72.247.155.60/is-cdn-ip --print=b


{
    "isCdnIp": true
}

Now you can check the geo-location of the IP address to verify if it is indeed in Madrid, Spain. 

Example 4: Print the Geo-location

$ http --auth-type edgegrid --auth devrel-admin: :/diagnostic-tools/v2/ip-addresses/72.247.155.60/geo-location --print=b


{
    "geoLocation": {
        "areaCode": null,
        "asNum": "20940",
        "city": "MADRID",
        "clientIp": "72.247.155.60",
        "continent": "EU",
        "countryCode": "ES",
        "county": null,
        "dma": null,
        "fips": null,
        "latitude": 40.4,
        "longitude": -3.68,
        "msa": null,
        "network": null,
        "networkType": null,
        "pmsa": null,
        "proxy": null,
        "regionCode": null,
        "throughput": "vhigh",
        "timeZone": "GMT+1",
        "zipCode": null
    }
}

We hope you enjoy using HTTPie v2 to explore Akamai APIs!

Javier GarzaJavier Garza is a developer evangelist at Akamai Technologies where he helps the largest companies on the internet run fast and secure apps by leveraging web performance, security and DevOps best practices. His life’s motto is: share what you learn, and learn what you don’t.