Introduction

The article explains the ‘requests’ library, it’s a simple HTTP library for Python, it’s very elegant and within a few lines of code, we can develop a proper REST client. In this tutorial, we will invoke the public REST API to check the current weather of any request city.

URL: https://goweather.herokuapp.com/weather/{city}

Installation

For installing the requests library, we can use either

Python
pip install requests

Or if you are using Anaconda, then simply find and install the 'requests' package.

GET

The most common HTTP method is GET, which indicates that the user is requesting a resource from the server. We will check the current weather of ‘New York’ right now using the ‘requests’ library.

Python
import requests
response = requests.get('https://goweather.herokuapp.com/weather/NewYork')
response

Output: <Response 200>

The .get method returns a Response object, which contains the results returned by the GET operation. 'status_code' returns the status of the request returned by the server, for example

Python
response.status_code // returns 200

If we don’t pass city in the request, the result is 404

Python
import requests
response = requests.get('https://goweather.herokuapp.com/weather/')
response

We can modify our code

Python
import requests
response = requests.get('https://goweather.herokuapp.com/weather/NewYork')
if(response.status_code == 200):
    print('Success: Response Found')
else:
    print('Failed: Response not found')

To get the response reason, we should be using 'response.reason' which in this case returns ‘OK’.

Python
response = requests.get('https://goweather.herokuapp.com/weather/NewYork')
response.reason

The 'status_code' only returns the status code, how about the Payload which is returned by the service, how to access that.

.content: The .content returns response in bytes,

Python
response = requests.get('https://goweather.herokuapp.com/weather/NewYork')
response.content
Code
'{"temperature":"+2 \xc2\xb0C",
"wind":"26 km/h",
"description":"Partly cloudy",
"forecast":[{"day":"1","temperature":"+2 \xc2\xb0C","wind":"14 km/h"},
{"day":"2","temperature":"+7 \xc2\xb0C","wind":"6 km/h"},
{"day":"3","temperature":"+7 \xc2\xb0C","wind":"13 km/h"}]}'

.text: using response.text, we get the response in String

Python
response = requests.get('https://goweather.herokuapp.com/weather/NewYork')
response.text
Code
'{"temperature":"+2 °C",
"wind":"26 km/h","description":"Partly cloudy",
"forecast":[{"day":"1","temperature":"+2 °C","wind":"14 km/h"},
{"day":"2","temperature":"+7 °C","wind":"6 km/h"},
{"day":"3","temperature":"+7 °C","wind":"13 km/h"}]}'

.json() gives us the response in JSON format

Python
response = requests.get('https://goweather.herokuapp.com/weather/NewYork')
response.json()
JSON
{'temperature': '+2 °C',
 'wind': '26 km/h',
 'description': 'Partly cloudy',
 'forecast': [{'day': '1', 'temperature': '+2 °C', 'wind': '14 km/h'},
  {'day': '2', 'temperature': '+7 °C', 'wind': '6 km/h'},
  {'day': '3', 'temperature': '+7 °C', 'wind': '13 km/h'}]}

response.headers(): returns the headers of the response, in dictionary format.

Python
response = requests.get('https://goweather.herokuapp.com/weather/NewYork')
response.headers
JSON
{'Server': 'Cowboy', 
'Connection': 'keep-alive', 
'Content-Type': 'application/json', 
'Date': 'Thu, 23 Dec 2021 18:00:08 GMT', 
'Content-Length': '239', 'Via': '1.1 vegur'}

To access any header element

Python
response.headers['Content-Length'] //239

Proper GET request

In general, we need to pass the headers and sometimes the parameters for sending a REST request, the header may contain any info like customerId, Accept sometimes versioning, we should supply such information in the request header, for doing that in requests library the format is

Python
response = requests.get(
    'https://goweather.herokuapp.com/weather/NewYork',
    headers={'Accept': 'application/json'},
)

For parameters, we should be passing ‘params’ in the request.

Other HTTP Methods

The other HTTP methods like ‘POST’, ‘PUT’, ‘DELETE’ has a very similar signature, although the current weather API we are using supports only GET operations for other operations to imagine a REST service called Employee Rest Service

Python
requests.post('/employee', data={"empName": "John"})
requests.put(('/employee/20', data={"empName": "John"})
requests.delete('/employee/20');

The requests library is extremely efficient and very elegant to work with, I recommend using it if you want to write your own REST client, irrespective of your service which is written in Java or Python.

Thank You for reading