how to send http request using python

How to Send HTTP Request Using Python

If you're into web development, working with HTTP requests is a common task. Python provides a simple way to send HTTP requests using its built-in requests library. Here are some ways you can send HTTP requests using Python:

Method 1: Using the GET method

The GET method is used for retrieving data from a server. You can use the requests.get() method to send a GET request in Python.

import requests

response = requests.get('https://jsonplaceholder.typicode.com/todos/1')
print(response.content)

In this code, we're sending a GET request to the 'https://jsonplaceholder.typicode.com/todos/1' URL and printing the response content. This will return a JSON response, which we can access using the response.content property.

Method 2: Using the POST method

The POST method is used for sending data to a server. You can use the requests.post() method to send a POST request in Python.

import requests

payload = {'name': 'John', 'age': 30}
response = requests.post('https://httpbin.org/post', data=payload)
print(response.text)

In this code, we're sending a POST request to the 'https://httpbin.org/post' URL with some data in the payload. We're printing the response text, which will show us the data that we've sent to the server.

Method 3: Using the PUT method

The PUT method is used for updating data on a server. You can use the requests.put() method to send a PUT request in Python.

import requests

payload = {'name': 'John', 'age': 40}
response = requests.put('https://httpbin.org/put', data=payload)
print(response.text)

In this code, we're sending a PUT request to the 'https://httpbin.org/put' URL with some data in the payload. We're printing the response text, which will show us the updated data that we've sent to the server.

Method 4: Using the DELETE method

The DELETE method is used for deleting data on a server. You can use the requests.delete() method to send a DELETE request in Python.

import requests

response = requests.delete('https://httpbin.org/delete')
print(response.text)

In this code, we're sending a DELETE request to the 'https://httpbin.org/delete' URL. We're printing the response text, which will show us the data that has been deleted from the server.