make a http request with a custom body as form data in python

To make an HTTP request with a custom body as form data in Python, you can use the requests library. Here's an example:

main.py
import requests

url = 'https://example.com'
data = {'key1': 'value1', 'key2': 'value2'}

response = requests.post(url, data=data)

print(response.content)
156 chars
9 lines

In this example, we imported the requests library and set the url variable to the URL we want to send the request to. We then created a dictionary data that contains the form data we want to send.

To make the actual request, we use requests.post and pass in the url and data parameters. This sends a POST request to the URL with our form data.

Finally, we print the response content returned from the server.

Note that requests.post automatically sets the Content-Type header to application/x-www-form-urlencoded, which is the standard way to send form data in an HTTP request. If you need to set a different content type or send files as well as data, you can use the files, headers, and json parameters of the requests.post function.

gistlibby LogSnag