How to create Get and Post request using Python requests module

Requests is open-source library to send http requests in Python. Though there is Python's built-in library, urllib2 but it lacks many capacities you need.

Requests provide simple and easy way to create HTTP or API requests. With requests, you can send most common HTTP requests, add headers and data, create authenticate requests etc.

In this article, we will install Python's requests module and create different requests with example. So let's start!

Install requests module

First install the requests library with Python pip package.

pip install requests

Once requests library is installed, you can import and use it.

Create request

First import the requests library in the begining.

import requests

Here is the simple Get request.

response_data = requests.get('https://website.com/get')

response_data variable stores all the response information from the request.

You can also make Post request with data.

response_data = requests.post('https://website.com/post', data = {'key': 'value' })

This way you can send all type of request.

response_data = requests.put('https://website.com/update', data = {'key': 'value'})
response_data = requests.delete('https://website.com/delete')
response_data = requests.options('https://website.com/get')

Sometimes you may want to send get request with query string. This is how you can send get request with query string.

data = {'key': 'value', 'new_key': 'new_value'}
response_data = requests.get('https://website.com/get', params = data)

If you want to pass header with request, then pass like this.

headers = {'Content-Type': 'Application/Json'}
response_data = requests.get('https://website.com', headers = headers)

If route is protected with authentication, then you can also add auth.

response_data = requests.post('https://website.com/post', data = {'key': 'value' }, auth = ('username', 'password'))

You can set time to stop request for wait.

response_data = requests.get('https://website.com/get', timeout = 5)

Of course, you can also pass image in request payload.

url = 'https://website.com/post'
file = {'file': open('document.pdf', 'rb')}

response_data = requests.post(url, files=file)
response_data.text

Getting response

After creating request you want to handle response data and response header. Here is how to get and handle data.

We can check the response status code with status_code.

response_data = requests.get('https://website.com/get')

http_status_code = response_data.status_code

You can also check response header.

response_header = response_data,headers

This will return all headers in dictionary format.

{
    'content-encoding': 'gzip',
    'transfer-encoding': 'chunked',
    'connection': 'close',
    'server': 'nginx/1.0.4',
    'x-runtime': '148ms',
    'etag': '"e1ca502697e5c9317743dc078f67693f"',
    'content-type': 'application/json'
}

You can also get specific header:

content_header = response_header.headers['Content-Type']
'application/json'

content_header = response_header.headers.get('content-type')
'application/json'

If a response contains some Cookies, you can get cookies this way.

response_data = requests.get('http://example.com/url')

response_data.cookies['example_cookie_name']

Congratlations! you have just learned to make basic HTTP requests. In the next article, we will discuss in details.

Tags:

Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

Or

No Comment