To fetch any data from a server you have to make a request from a data which is known as an API request. It basically works as- you make a request to an API server and it responded to your request.
To get any data using API you must have to make an API request. There are many types of requests in this library. We are going to work with some major ones. Let's see how to make API requests in python.
To make API requests in python we need to have some tools. In python, there is one common library that is used to make a request and work with API. Requests library helps us in working with API.
It is not a standard library of python, we have to install this library manually. Here is a tutorial to do that.
To install the Requests library use this command in your terminal.
|
If you are using conda then go for this command
|
Once you have installed this library. Let's import it into our program to use it. Follow this import command in your program.
|
Now we are all set to work with this library. Let's get started.
There are different types of requests we can make. The GET request is mostly used to retrieve data. We are going to use the GET request in our program as we are going to make API requests.
When we will send an API request the API will ping back with a response code that tells us whether our request was successful or not. If there will be something wrong then it will be mentioned in a response code.
Now to send a GET request, we will use the requests.get() function which contains only one argument (In an argument we will add the URL from which we want to make a request). Let's start a coding part.
|
|
|
Have a look at these status codes, you will notice that codes starting with '4' are some types of errors, and starting with '2' is a successful request. These are some of the major response codes which usually occur. To know more about the status codes go through this documentation.
In order to make a successful API request, you must read the API documentation that will help you working with API. Documentation looks so difficult but once you started going through it, you will know how to handle API easily.
Now in the above example, we were working with "Open Notify API". It is an open-source API that provides the data of NASA and space. For example, it will tell you about the current location of the international space system and many more. We will make a request from this server in this tutorial.
There can be different APIs in one server. Each of these APIs is known as endpoints. It is usually a point, where we want to send our request. For example,
http://api.open-notify.org/iss-now.json, which returns data about the current location of the international space station. This API doesn't take any type of inputs. Let's know about this API implementation in brief by making a request in python.
We will use the GET request to our endpoint using the request library.
|
By making this request we will get the response code which tells us about the status of our request.
200
Here the status code we received is '200' which means that our request was successfully made. Now we will use the response.json() method to get the data we received from the server.
[{'message': The ISS is currently over -33.127° N, -76.746° E, 'craft': 'ISS'}] |
JSON full form is javascript object notation. It is a language of APIs to fetch the data we get from the servers. In simple words, it is just a way to encode the data we received from the servers in a primary format. It seems difficult to work with JSON but you can easily master the JSON language with this quick tutorial.
Python Example on Read JSON file in Python
You will notice that the above data we received from the server is in some type of format which is quite similar to Python dictionaries and lists. This is JSON language. Here is a small example to explain you JSON.
[
{
"message": "The ISS is currently over -33.127° N, -76.746° E",
"craft": "ISS"
}
{
"message": "The ISS is currently over -43.227° N, -26.546° E",
"craft": "ISS"
}
]
|
The JSON library has 2 main functions:
resp = requests.get('https://todolist.example.com/tasks/')
if resp.status_code != 200: # This means something went wrong.
raise ApiError('GET /tasks/ {}'.format(resp.status_code))
for todo_item in resp.json():
print('{} {}'.format(todo_item['id'], todo_item['summary']))
|
In this code first of all we have made a request from the server. If the API request is successful we can print the data we received back from the server. Else it will print the response code which will tell us about the error we are facing.
Article Contributed By :
|
|
|
|
529 Views |