What is a REST API?

API stands for "application program interface". It's just a documented method of interacting with someone else's service. For example, Google has an API for Gmail. And an API for Calendar. And APIs for just about everything else they do. They're pretty good at them, actually.

If you want to interact with someone else's service, you generally use an API. It sure beats calling Jawbone up on the phone and saying "oh hey, can you email me my activity data for the last month? Thanks".

REST is just a pattern for making APIs. I'd tell you what it stands for, but it doesn't matter. It's just a standard pattern for naming resources that service providers.

In the Gmail API example, you can interact with your email drafts. To do this, Google follows the REST pattern by giving you a URL that looks like the below URL. (Side note, almost all REST APIs use HTTP. They don't have to, but all of the ones that matter do).
'https://www.googleapis.com/gmail/v1/users/userID/drafts'

Want to list your drafts? Send a GET request to that URL.
Want to create a draft? Send a POST request to that URL.
Want to read a specific draft? Send a GET request to the URL:
'https://www.googleapis.com/gmail/v1/users/userId/drafts/draftId'

Want to update your draft? Send a POST request to the URL above.
Want to delete your draft? Send a DELETE request to the URL above.

What if you wanted to do all of the same actions(list, create, read, update, delete) to a different resource, say Gmail Labels?

Well, because they're using the REST pattern for their API, you can easily see the pattern for listing, creating, reading, updating, and deleting Labels. It's just at a different URL:
'https://www.googleapis.com/gmail/v1/users/userId/labels'

So what's a REST API? It's a standard pattern for an interface for another service that you can use programmatically (from a computer program).

posted @ 2021-01-12 09:56  咕咕鸟GGA  阅读(80)  评论(0编辑  收藏  举报