Part I : RESTful API

In this post I will be covering:

  1. What is a Restful API
  2. Methods / verbs
  3. Implementing API calls

API’s

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.

api

This is what API does, Its an application Programming interface that connects components.

RESTFUL API

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 .

API METHODS/VERBS

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

IMPLEMENTATION

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….!!

  1. Importing the requests module1
  2. The url holds the endpoint from where i want to retrieve the information.2
  3. Implementing GET, Once the url is passed to the request module we get the data in form of a dictionary( key value pair)3
  4. Now implementing POST. we create a dictionary and send it. Post is creating a new resource. my_data is the dictionary. 4
  5. 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.5
  6. Delete is deleting the resource. Once i print response.json() it displays empty dictionary, it has no value. 6

Hope this was fun. Stay tuned for more on API, till then happy coding.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s