Python – Custom HTTP Requests


Python – Custom HTTP Requests


”;


The Hypertext Transfer Protocol (HTTP) is a protocol used to enable communications between clients and servers.
It works as a request-response protocol between a client and server. The requesting device is known as the client
and the device that sends the response is known as server.

The urllib is the traditional python library which is used in python programs to handle the http requests.
But now there is urllib3 which does more than what urllib used to do. We import the urllib3 library to see
how python can use it to make a http request and receive a response. We can customize the type of request by
choosing the request method.

Pip install urllib3

Example

In the below example we use the PoolManager() object which takes care of the connection details of the http request.
Next we use the request() object to make a http request with the POST method. Finally we also use the json library
to print the received values in json format.

import urllib3
import json

http = urllib3.PoolManager()
r = http.request(
    ''POST'',
    ''http://httpbin.org/post'',
    fields={''field'': ''value''})
print json.loads(r.data.decode(''utf-8''))[''form'']

When we run the above program, we get the following output −

{field'': value''}

URL Using a Query

We can also pass query parameters to build custom URLs. In the below example the request method uses the values in the query string to complete the URL which can be further used by another function in the python program.

import requests
 
query = {''q'': ''river'', ''order'': ''popular'', ''min_width'': ''800'', ''min_height'': ''600''}
req = requests.get(''https://pixabay.com/en/photos/'', params=query)
 
print(req.url)

When we run the above program, we get the following output −

https://pixabay.com/en/photos/?q=river&min_width=800&min_height=600&order=popular

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *