In this post I will be covering:
- What is a Restful API
- Methods / verbs
- Implementing API calls
To explain API, i’ll use the Waiter analogy. Once you go to the restaurant and you are seated, the waiter comes to you with a Menu, Menu is a list of stuff you can order. So You are an API consumer, the kitchen is the response ( a database or a server that holds required data), whatever you order is API call and waiter is the API, who goes to the kitchen and gives the order to the chef.
This is what API does, Its an application Programming interface that connects components.
We know what is an API, now what are restful Api’s . REST stands for Representational State Transfer, its an architectural design that defines set of constraints of how web services should be developed. So therefore web-services that follows the guidelines set by REST are RESTful Services. For more details on Architectural properties this is the link .
Verbs help us to interact with the API, most common ones are:
- GET: Retrieve resource
- PUT: Modify already available resource
- POST: Create a new Resource
- DELETE: Delete Resource
Now we are going to implement all these methods mentioned above. I am using python 3.6. The website i am using is :https://jsonplaceholder.typicode.com/ . Its pretty good if you want to sharpen you’re skills with API’s.
The python module i will be using is requests. And the data I am looking at is Json, you can also go along with xml using the same library. Here we go….!!
- Importing the requests module
- The url holds the endpoint from where i want to retrieve the information.
- Implementing GET, Once the url is passed to the request module we get the data in form of a dictionary( key value pair)
- Now implementing POST. we create a dictionary and send it. Post is creating a new resource. my_data is the dictionary.
- To update/ modify we use PUT. I have updated the url to posts/100 and i am changing the value of post with id:100 to my_data.
- Delete is deleting the resource. Once i print response.json() it displays empty dictionary, it has no value.
Hope this was fun. Stay tuned for more on API, till then happy coding.