Image Image Image Image Image Image Image Image Image Image

Being Brunel |

Scroll to top

Top

One Comment

Web APIs: An Engineering Introduction

Web APIs: An Engineering Introduction

I‘m sure I don’t have to tell you that the internet is awesome. Or indeed that it’s everywhere. But its ubiquitousness might surprise you. Many devices and applications talk “internet” in their APIs (application programming interfaces), which is becoming a sort of lingua franca of tech, and knowing how it works will help you leverage more and more sources in civil engineering.

The internet boils down to one protocol: HTTP. And for all the average user needs to know, it’s a language of five verbs. You can GET something, POST something new, PUT an updated version of something, DELETE something unwanted and PATCH something with a few changes.

Congratulations, you now speak HTTP. In fact, when you browsed to this page you actually GET it. And when you fill your email address to sign up for updates you POST it to me. When you fix that badly spelt YouTube comment you PUT the changed version and when you delete that drunk photo on Facebook you DELETE it. (PATCH is, alas, not all that common unless you’re working on big documents.)

Just knowing the HTTP verbs will get you pretty far. But as an English teacher will tell you, verbs just aren’t enough. You need need an object to your sentence; something to do you verb on.

Years ago during the great API wars there were many competing standards, but these days pretty much every web API you interact with will be RESTful.

REST is a convention. It sits on top of the HTTP verbs to make it stupidly easy to express what you want. The idea is simple, everything you GET, POST, etc. to should be in the form /things/identifier. So, in a typical RESTful pattern:

  • GET /things/ – Gets all the things
  • GET /things/1 – Gets the first thing
  • GET /things/1/bits/2 – Gets the second bit of the first thing
  • POST /things/ – Adds a new thing
  • PUT /things/1 – Updates the first thing with some new data
  • DELETE/things/1 – Destroys the first thing!

There’s one more thing you need to be versed in before you can classify yourself as a Web Genius, and that’s how to ask a question. By convention, questions (or API query strings as people pretending to be technical call them) are asked using a ?, followed by a & separated series of options=values. So:

  • GET /things/?colour=red – Gets all the red things

Unfortunately there’s no REST-like discoverability for what options you can use; you’ll have to just check the documentation or see what you can work out by looking at the query strings of others. Wrapping up all we’ve learned so far:

GET https://twitter.com/hashtag/engineeringisart?f=images

  • GET‘s
  • From https://twitter.com
  • The /hashtag/engineeringisart
  • Displaying only the ?f=images

And that’s the basics of how pretty much every internet enabled application speaks. In later posts I’ll go through some of the nuances like authentication and data formats, as well as talking about how you can use this new found knowledge to leverage the web.

Submit a Comment

Leave a Reply

Trackbacks

  1. […] week I talked at length about the wonderful world of the Web API, but completely failed to provide any details of how you can start playing with them. Well today […]