Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Requests Module in Python

Requests Module in Python

requests Module in Python

The requests module in Python is a simple and elegant HTTP library for sending HTTP requests. It abstracts many of the complexities involved in handling HTTP requests and responses, making it easier to interact with web APIs, download files, and perform web scraping.

Installing the requests Module

If you don't have the requests module installed, you can install it using pip:

pip install requests

Basic HTTP Requests with requests

The requests module allows you to send various types of HTTP requests like GET, POST, PUT, DELETE, etc. Below are examples for each of the common HTTP methods.


1. Sending a GET Request

A GET request is used to retrieve data from a server.

import requests# Sending a GET request to a URLresponse = requests.get('https://api.github.com')# Printing the status code of the responseprint(response.status_code)# Printing the content of the responseprint(response.text)
  • response.status_code: The HTTP status code returned by the server (e.g., 200 for success).

  • response.text: The text content of the response.


2. Sending a POST Request

A POST request is used to send data to a server (for example, when submitting a form).

import requests# URL for sending dataurl = 'https://httpbin.org/post'# Data to be sent in the POST requestdata = {'name': 'John', 'age': 30}# Sending a POST requestresponse = requests.post(url, data=data)# Printing the status codeprint(response.status_code)# Printing the response JSON (if any)print(response.json())
  • response.json(): Returns the response as a JSON object (if the response is in JSON format).


3. Sending a PUT Request

A PUT request is used to update an existing resource on the server.

import requests# URL to send the PUT requesturl = 'https://httpbin.org/put'# Data to updatedata = {'name': 'John', 'age': 31}# Sending a PUT requestresponse = requests.put(url, data=data)# Printing the responseprint(response.text)

4. Sending a DELETE Request

A DELETE request is used to delete a resource on the server.

import requests# URL to send the DELETE requesturl = 'https://httpbin.org/delete'# Sending a DELETE requestresponse = requests.delete(url)# Printing the status code and responseprint(response.status_code)print(response.text)

5. Sending a Request with Parameters (Query Parameters)

You can pass query parameters in the URL using the params argument.

import requests# URL with query parametersurl = 'https://httpbin.org/get'# Parameters to be sent with the GET requestparams = {'name': 'John', 'age': 30}# Sending a GET request with parametersresponse = requests.get(url, params=params)# Printing the responseprint(response.url)  # The full URL with query parametersprint(response.text)  # The response text

6. Sending a Request with Headers

You can send custom headers with your HTTP request.

import requests# URL to send the GET requesturl = 'https://httpbin.org/headers'# Custom headersheaders = {'User-Agent': 'MyApp/1.0'}# Sending a GET request with headersresponse = requests.get(url, headers=headers)# Printing the response JSONprint(response.json())

7. Handling Response Status Codes

You can check the status code to determine the success or failure of your request.

import requests# URL to send the GET requesturl = 'https://httpbin.org/status/200'# Sending a GET requestresponse = requests.get(url)# Checking if the request was successfulif response.status_code == 200:    print("Request was successful!")else:    print(f"Request failed with status code: {response.status_code}")

8. Handling Timeouts

You can set a timeout for your request to prevent it from hanging indefinitely.

import requests# URL to send the GET requesturl = 'https://httpbin.org/delay/5'  # This URL will delay the response by 5 secondstry:    # Sending a GET request with a timeout of 3 seconds    response = requests.get(url, timeout=3)except requests.Timeout:    print("The request timed out.")else:    print("Request was successful!")

9. Handling JSON Data

The requests module makes it easy to send and receive JSON data.

Sending JSON Data

import requestsimport json# URL to send the POST requesturl = 'https://httpbin.org/post'# Data to be sent (as a dictionary)data = {'name': 'John', 'age': 30}# Sending a POST request with JSON dataresponse = requests.post(url, json=data)# Printing the responseprint(response.json())

Receiving JSON Data

import requests# URL to send the GET requesturl = 'https://api.github.com'# Sending the GET requestresponse = requests.get(url)# Parsing the response as JSONjson_data = response.json()# Printing the JSON dataprint(json_data)

10. Authentication

If the API requires authentication, you can pass the credentials using the auth parameter.

import requests# URL for authenticationurl = 'https://httpbin.org/basic-auth/user/pass'# Sending a GET request with authenticationresponse = requests.get(url, auth=('user', 'pass'))# Printing the status code and responseprint(response.status_code)print(response.text)

11. Uploading Files

You can also upload files via a POST request.

import requests# URL to send the fileurl = 'https://httpbin.org/post'# Files to uploadfiles = {'file': open('myfile.txt', 'rb')}# Sending a POST request with a fileresponse = requests.post(url, files=files)# Printing the responseprint(response.text)# Close the filefiles['file'].close()

Conclusion

The requests module is a powerful and easy-to-use library for making HTTP requests in Python. It simplifies common tasks like making GET, POST, PUT, and DELETE requests, handling parameters, headers, timeouts, and working with JSON data. It is widely used for interacting with web APIs and scraping data from the web.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql