Requests - How Http Requests Work?

Python’s Requests is a HTTP library that will help us exchange data between the client and the server. Consider you have a UI with a form, wherein you need to enter the user details, so once you enter it, you have to submit the data which is nothing but a Http POST or PUT request from the client to server to save the data.

When you want the data, you need to fetch it from the server, which is again a Http GET request. The exchange of data between the client when it requests the data and the server responding with the required data, this relationship between the client and the server is very important.

The request is made to the URL given and it could be a secure or non-secure URL.

The request to the URL can be done using GET, POST, PUT, DELETE. The most commonly used is the GET method, mainly used when you want to fetch data from the server.

You can also send data to the URL as a query string for example −

So here, we are passing id = 9 and username = Delphine to the URL. All the values are sent in key/value pair after the question mark(?) and multiple params are passed to the URL separated by &.

Using the request library, the URL is called as follows using a string dictionary.

Wherein the data to the URL is sent as a dictionary of strings. If you want to pass id = 9 and username = Delphine, you can do as follows −

payload = {'id': '9', 'username': 'Delphine'}

The requests library is called as follows −

res = requests.get('', 
params = payload')

Using POST, we can do as follows −

res ='', data =
{'id':'9', 'username':'Delphine'})

Using PUT

res = requests.put('', data =
{'id':'9', 'username':'Delphine'})


res = requests.delete('')

The response from the Http request can be in text encoded form, binary encoded, json format or raw response. The details of the request and response are explained in detail in the next chapters.

Kickstart Your Career

Get certified by completing the course

Get Started