Hi everyone! Today I’m going to be talking about restful APIs.
If you have used any modern websites then chances are you have interacted with a website that uses REST. Even YouTube uses restful URLs on its site. But what exactly is REST? For starters REST stands for Representation State Transfer. It is just a fancy way of saying that a server responds to Create, Read, Update and Delete requests in a standard way. The idea behind REST is to treat all server urls as access points for various resources on the server.
For example. in this url — http://example.com/users — “users” represents the resource the server is exposing. As mentioned earlier, REST needs a way to create Read, Update and Delete this resources and it does so with the following five urls
The first two urls don’t have an id, so they act on the entire users resource, while the last three urls do have an id in their url and that acts on a single user resource but, as you can notice, there is only two distinct urls. This is because uses four basic http actions — Get, Post, Put and Delete to determine what to do with each url. If we add those actions to each url it is much easier to see what each url does
The first url is the “get users” url and it is used to get a list of all users. In REST, when a get url does not have an id it acts upon entire resource and will always return a list of every item in that resource. The get action in REST corresponds with reading data.
The second url, which is almost identical to the first, is used to create a new user. In REST, the post action corresponds with creation and should always be used on the entire resource by not using an id in the url.
The third url is another get url, but this url is only for getting a single user based on the id that’s in the url. The id part of the url is used to determine which resource from the collection of resources it should act upon. In case if this url, it is used to return a user with that id.
The fourth url is the most confusing of them all. It is used to update a user with the given id. The put actions in REST corresponds with update and works very similarly to post but instead of a crating a new resource it updates an existing resource.
Lastly, we have the most straight-forward url of them all which is for deleting a user with a specific id. The delete action in REST does exactly what you would think — it deletes the resource with the given id.
I hope you found my post useful! Happy coding!