Good day learner,
We will proceed with learning about APIs today. Specifically, it will be on how we are able to send instructions to the server to make changes to the data. Also to be covered are how the server communicates back the success of these changes.
This is the 3rd section of our 4-part series on APIs. In case you missed it, here are the 1st part and 2nd part of our 4 part series.
What are methods ?
In the API arena, methods are instructions sent to the server. These help to indicate or tell the server what to do. Different kinds of methods are usually used to relay for different kinds of instructions to the server.
Request methods
We have four main methods or ways of instructing a server to make changes to the data. These are the
- GET method
- POST method
- PUT method
- DELETE method
To use all these methods, these should have been created on the web server in question. Different programming languages implement this in different ways. However, there are only four methods.
The GET method
Picking up information from a web server is the most common reason why APIs are exposed. Hence the GET method is used to retrieve data from the server. That is for reading only purposes. Hence do not use it to alter data, but only to retrieve it.Examples of how it might look in the URL could be
*/users : list all users
*/ users/1 : list info for user with ID 1
For multiple parameters, I have seen the GET request method being used with the parameters in the URL.
For example.
https://www.agenu.com/userDetails?fname=Eli&lname=Agbenu
I would recommend that when passing long parameters or binaries use the POST request method. It would be better to send these as parameters in the post body.
The POST method
To be able to accept new information is another reason why APIs are exposed. As long as the proper and correct authorization is passed to the web server, then the API would allow you to make changes based on the data passed.
Consequently, the POST method is used to create data on the server. It is the second most used method of all the four methods. In the URL, you might see the path below.
*/users : create a new user
In some situations, it could also be used for updating and deleting records on the server.
The PUT method
From time to time, you may need to update the existing data you have on the server. The correct method to use for this is the PUT method.
However, based on the programmer’s preference, he may use other methods (notably the POST) method to request for the update of information.
In the URL, you might see the path below.
/users/1 : update user with ID 1
The DELETE method
Finally, the least used of all the four methods is the DELETE method. This is used to delete data on the server. Hence use it with caution.
For example, you might see it in the URL represented below.
/user/1 : delete user with ID 1
HTTP codes
When you send data or retrieve data from the server, we need to have feedback on how the web server received our requests. We need feedback of some sort. The HTTP codes are meant for this.
HTTP codes are used for indicating specific errors and displaying information. Usually, everyone is used to specific types of errors denoting specific types of errors. Below is a brief list of some of the most common HTTP status codes.
2xx – success codes
- 200 – OK
- 201 – Created
- 202 – Accepted
3xx – redirection codes
- 301 – moved permanently
- 304 – Not modified
4xx – client-side error
- 400 = Bad request
- 401 = Unauthorized
- 402 = Payment required
- 403 = Forbidden
- 404 = Not found
5xx – Server-side error
- 500 = Internal server error
- 501 = Not implemented
- 503 = Service unavailable
Conclusion
We were able to go through a very key part of how APIs work. We went through both the methods used and the HTTP status codes returned upon sending a request. I would encourage you to re-read it. It should help a lot of the concepts stick.
Kindly send questions on areas where you may want more clarity on. In the next and final post in this series, we will look at simple examples of how exactly to write the programming codes that create all of what we have discussed before.
Until we meet again later, keep learning and soaring.
Thank you.