9, Jan 2023
How do I make an HTTP request in python?
How do I make an HTTP request in python?

In Python, you can use the requests library to make HTTP requests. Here’s an example of how to send a GET request and print the response

import requests

response = requests.get('https://www.example.com')

print(response.text)

The requests.get() function sends an HTTP GET request to the specified URL and returns a response object. The response object has a text attribute that contains the response body as a string.

You can also use the requests.post() function to send an HTTP POST request, or use any of the other HTTP request methods supported by the requests library (e.g. requests.put(), requests.delete(), etc.).

Here’s an example of how to send a POST request with a JSON payload in the request body:

import requests

response = requests.post('https://www.example.com/api/create', json={'key': 'value'})

print(response.json())

The requests.post() function takes a json parameter that specifies the JSON payload to send in the request body. The response.json() function parses the JSON response and returns it as a Python dictionary.