Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.2 Routes and Endpoints

In this lesson, I'll explain the difference between routes and endpoints and how they work to power the REST API.

2.2 Routes and Endpoints

Hey, folks, and welcome back to the course introducing the WPS API. I'm your instructor Bilal Shaheed. Another important concept when working with JS API is routes and endpoints. They are often confused for each other, but in reality they are quite different. In this lesson, we will take a look at what they actually are and how they differ from each other. While routes are a way to point to a particular resource, endpoints are functions that perform several actions on these resources. These actions can be retrieving, creating, updating or deleting a particular resource. Alternatively, we can say that and endpoint CRUD mattered that performs a specific task. And since we are talking about performing CRUD operations, these endpoints are closely associated with HTTP verbs. Let me demonstrate by an example. Here, we have our get points to the post resource in the WordPress for JPI. This route has two endpoints for retrieving and creating posts. So the first endpoint triggers the get method that is responsible for retrieving a collection of posts. And the second endpoint triggers the post method that is responsible for creating a new post. And this endpoint also contains the functionality for checking for authentication and validating the provided data before it actually creates a post. Similarly, this is the route that points to a particular post having an ID of 10. This route has three endpoints responsible for creating, updating and deleting and that particular post. The first endpoint uses the GET method, and it retrieves a post having an ID of 10. The second endpoint uses the PUT method and is responsible for updating this post with new data. And since a updating a post requires authentication, the functionality to authenticate a user would be implemented here in this endpoint. It's worth noting that some APIs use both put and post methods for updating a resource, and this is also the case with JSWPSAPI.. So we can send a put or a POST request to a particular source, and both request will have the same effect and they will update the resource with new data. The third, and the last endpoint is used for deleting the posts having an ID of 10, and it uses the delete method. Deleting a post also requires authentication, so the user would need to post proper credentials for performing this operation. In these examples, we use the post resource, but these concepts will remain the same whether we are working with posts, pages, users or any other resource supported by the WPS API. So that was a pretty basic overview of what routes and endpoints are and how they work. In the next lesson, we will take a look at JSON that is presenting data and is used quite heavily in the WPS API. See you in the next video.

Back to the top