Understanding REST APIs


Understanding REST APIs

Corey
Hey Simon, welcome to the podcast! Today we're diving into something super common in web development but often misunderstood: REST APIs. I know it can be a really confusing topic when you're just starting out, and many explanations online are far too complex.

Simon
Exactly, Corey. It's one of those things that sounds intimidating, but when broken down, it's quite logical. My goal, and I know yours too, is to explain these complex web concepts in plain English, without all the jargon. Let's make it accessible for everyone.

Corey
Absolutely. Before we even get to REST, we should clarify what an API is in general. API stands for Application Program Interface. It's a very broad term, covering everything from your computer's operating system to smartphone apps and even smart home devices.

Simon
Right, it's essentially a contract between two pieces of software. One piece of software provides an interface, and the other uses it to request specific information or functionality. Think of it as a structured request leading to a structured response, like a clear give-and-take.

Corey
That's a great way to put it. To make it even clearer, let's use an analogy that I really like. Imagine you're at a restaurant. You, the customer, are like the client-side web application, making a request for food.

Simon
And the kitchen is the server, the service that processes your order. The waiter in this scenario is the API. The waiter is designed to take your order in a specific format – a menu item – and then brings back your meal, which is the data or response you requested.

Corey
Perfect! So, an API is basically the messenger or the waiter between different pieces of running software. It facilitates communication. Here's another simple one: think of a child's shape sorter toy. The toy itself is like the API.

Simon
Ah, I get it! The toy is formatted to accept specific shapes – circles, squares, triangles. If you try to put in an octagon, it won't fit, and you'll get an error, just like an API would reject an improperly formatted request. The shapes are like the API standard, say JSON or SOAP.

Corey
Exactly. And if we imagine the toy had an output for the shapes once they went in, that would be the response. So, an API defines the valid requests and the expected responses. Hopefully, that gives everyone a solid foundation for what an API is.

Simon
Now that we've got the general API concept down, let's talk about REST specifically. REST stands for Representational State Transfer. It's an architectural style for designing networked applications, primarily relying on a stateless client-server communication protocol.

Corey
And in almost all cases, that protocol is HTTP, which most of our listeners will be familiar with. HTTP is the fundamental communication method of the internet; every time you load a webpage, your browser makes an HTTP request to a server somewhere.

Simon
While other protocols are technically possible with REST, HTTP is overwhelmingly the most used because it provides the necessary delivery methods. REST treats server-side objects, like a blog post in a database, as 'resources' that can be manipulated.

Corey
These manipulations are what we call CRUD operations: Create, Read, Update, and Delete. We can create new blog posts with a POST request, retrieve them with a GET request, update them with a PUT request, and delete them with a DELETE request.

Simon
What makes REST so powerful and awesome is that it operates using just HTTP and usually a standard like JSON for data formatting. This means virtually any programming language can use it, as most modern languages can make HTTP requests.

Corey
Whether you're using PHP, JavaScript, Python, Java, or Ruby on Rails, they're all perfectly capable of interacting with restful interfaces. So, don't get lost in the terminology; an API is the messenger, and REST lets us use standard HTTP requests to format those messages.

Simon
You might also hear the term 'RESTful API,' which simply means an API that conforms to the REST architectural constraints. So, REST API and RESTful API are essentially interchangeable terms for the same thing.

Corey
Right, they refer to the same concept. Now, let's look at the specific HTTP methods or requests that can be made to a server through REST. The most common, and one you use daily, is the GET request. Your browser makes GET requests every time you visit a website.

Simon
GET requests are used to retrieve data from a specified resource. Then we have POST requests. You're also using these every day, typically when you fill out and submit a web form. POST requests send data to the server to create a new resource.

Corey
It's important to note that while forms can technically use GET, it's not secure because the submitted data is visible in the URL. POST is the preferred method for sending data that needs to be kept private or when creating new resources. HTML form tags use 'action' and 'method' attributes.

Simon
Exactly. The action is the page you're submitting to, and the method is either POST or GET. Next up is PUT. A PUT request is used to update an existing, specified resource. You'd typically send it to an endpoint with the resource's ID, like an ID for a blog post.

Corey
The server needs to know which specific resource you intend to update. Unlike GET or POST, you can't make a PUT request directly from a standard HTML form. You'd typically use JavaScript with AJAX, or a framework like Angular with its HTTP module.

Simon
And then there's DELETE, which is pretty self-explanatory. A DELETE request removes a specified resource from the server. Again, you'd include the resource's ID in the request so the server knows exactly what to delete.

Corey
While there are other HTTP methods like HEAD, OPTIONS, and PATCH, GET, POST, PUT, and DELETE are the primary ones you'll encounter with REST APIs for typical CRUD operations. HEAD requests, for instance, are like GET but only return header info, not the body.

Simon
OPTIONS can show you the supported methods for a resource, and PATCH is for updating partial resources. But for most applications, focusing on the main four is key. Let's look at some endpoint examples now, which are the URIs or URLs our HTTP requests are sent to.

Corey
Imagine a sample API at mysite.com/api. To get a list of all users, you'd send a GET request to /api/users. To get a specific user, say user with ID 1, you'd make a GET request to /api/users/1.

Simon
You might also see variations like /api/users/details/1 or /api/users/show/1. To add a new user, you'd send a POST request to /api/users, sending the new user's data in the request body. Notice, GET and POST can use the same endpoint, but the different methods distinguish their intent.

Corey
That's a crucial point. For updating user 1, you'd send a PUT request to /api/users/1, again with the updated data. Alternatively, you might see /api/users/update/1. And to delete user 1, a DELETE request to /api/users/1, or perhaps /api/users/delete/1.

Simon
These examples use clear, predictable URLs, which is a hallmark of REST. Now, if we use these endpoints without any authentication, they're considered public or open APIs. But often, you need to authenticate before using them, especially for write operations or higher request volumes.

Corey
Authentication might involve registering your application with the provider's website, and sometimes even paying for access to certain data. A common way this is implemented is through OAuth, which involves getting an access token and sending it with your requests.

Simon
If you try to make a request without the required token, you'll typically get an unauthorized error. A great example of this is the GitHub API, which is fantastic for beginners. You can use it without authentication, but there's a rate limit.

Corey
If you exceed about 100 requests per hour without authorizing, you'll get an error and won't be able to fetch data. There are a few ways to send that token. Using a tool like cURL, you can send it in the HTTP header, setting an 'Authorization' value to your token.

Simon
Alternatively, you can send the token as a parameter directly in the URI. Another common method is to send a generated client ID and client secret as parameters, rather than the token itself, which helps keep sensitive information secure.

Corey
Let's take a quick look at the GitHub API documentation to see this in action. They have excellent documentation at developer.github.com/v3, detailing endpoints, methods, and authentication. Most public APIs will offer similar resources.

Simon
It clearly outlines the HTTP verbs like GET, POST, PUT, DELETE, and explains authentication methods, including sending tokens in headers or as parameters. It even shows how to handle pagination, allowing you to request a specific number of items per page.

Corey
For instance, to get a single user's details, you'd make a GET request to /users/:username, where ':username' is a placeholder for the actual username. The documentation shows the expected JSON response, including the user's ID, avatar URL, repo count, and more.

Simon
And if you're authenticated as a specific user, you can even make requests to update your own profile information. To get all users, it's just a GET request to /users. It's incredibly straightforward once you understand the structure.

Corey
Now, you can test these requests directly in your code, but tools like Postman are invaluable. Postman is a Chrome extension where you can enter your endpoint, select the request type, add headers for authorization, or send data in the body for POST/PUT requests.

Simon
With Postman, if I make a GET request to api.github.com/users, I get an array of user objects: usernames, IDs, URLs. If I want a specific user, say myself, I just append my username to the URL, and it returns my detailed user object.

Corey
Exactly. And if you make too many requests without authenticating, GitHub will give you an 'API rate limit exceeded' error. To overcome that, you'd register your application on GitHub, which gives you a client ID and client secret.

Simon
Then, you just include those as parameters in your requests, like client_id=YOUR_ID and client_secret=YOUR_SECRET, and you're authenticated. You can then make more requests without hitting the rate limit, allowing your application to function smoothly.

Corey
So, that's a comprehensive overview of what REST APIs are and how they work in the real world. Hopefully, this makes a complex topic much clearer for everyone. Understanding these concepts is fundamental for building modern web applications.

Ask anything
We've read this whole study set, so questions about it get the best answers.