Python HTTP GET and POST with examples

In today’s post, we will write code for performing Python HTTP GET and POST with examples

We shall create a basic HTTP request using a Python instance.

Today in this article, we will cover below aspects,

Getting started

Please install the ‘requests’ library.

pip install requests

HTTP GET Request

Requests allow you to send  HTTP/1.1 requests and it is powered by urllib3. with added features like Keep-alive and HTTP connection pooling features.

GET Example

import requests

r = requests.get('https://www.thecodebuzz.com/freebooks)

GET Example using Headers

If using Authentication headers then please use the same using headers and request object.

Example

headers = { 'Accept' : 'application/json',
'Content-Type' : 'application/json'}
response = requests.get(url,headers = headers )
print(response.status_code)
print(response.text)

HTTP POST Request

HTTP Post method allows sending the Message Body as per HTTP specification.

Similarly, a message body can be sent for requests like PUT or PATCH, etc.

Python POST Example

Below is the sample POST request where we are sending the message body using json_data which is JSON format data.

      headers = {'Accept' : 'application/json', 
                  'Content-Type' : 'application/json'}
    
       response = requests.post(url = url,json = json_data)
       print(response.status_code)
       print(response.text)

Python – Authentication Headers in Request

JSON Web Token( JWT) is an open standard used for securely transmitting information between parties as a JSON object.

JSON Web Tokens are very useful for various scenarios like authorization purposes or Information exchange using digitally signed key-value pairs.

Please define HttpHeaders to be used for JWT bearer token as below,

Example

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJleHAiOjE1OTU0NzE5MjMsImlzcyI6Imh0dHBzOi8vbG9jYWxob3N0OjQ0MzQxIiwiYXVkIjoiaHR0cHM6Ly9sb2NhbGhvc3Q6NDQzNDEifQ.
FdHX7Ycs_Z5zwAQkRD9iyVRPsgS42QqC8yQ9EKYnVC4

The client should send an Authorization header with the Bearer schema as below.

Authorization: Bearer <token>

Let’s define HttpHeaders to be used for JWT bearer token as below,

     headers = {'Authorization' :"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJleHAiOjE1OTU0NzE5MjMsImlzcyI6Imh0dHBzOi8vbG9jYWxob3N0OjQ0MzQxIiwiYXVkIjoiaHR0cHM6Ly9sb2NhbGhvc3Q6NDQzNDEifQ.
FdHX7Ycs_Z5zwAQkRD9iyVRPsgS42QqC8yQ9EKYnVC4", 
                  'Accept' : 'application/json', 
                  'Content-Type' : 'application/json'}
    
       response = requests.post(url = url,json = json_data)
       print(response.status_code)
       print(response.text)

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

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