make a http request with a custom body as html in python

You can make a HTTP request and send a custom HTML body in Python using the http.client module. Here's an example:

main.py
import http.client

# set up the connection
conn = http.client.HTTPSConnection("www.example.com")

# define the headers
headers = {"Content-type": "text/html"}

# define the custom HTML body
body = "<html><body><h1>Hello, World!</h1></body></html>"

# make the request with the custom body
conn.request("POST", "/path/to/api", body, headers)

# get the response
res = conn.getresponse()

# print the response status and body
print(res.status, res.reason)
print(res.read().decode())
482 chars
21 lines

In this example, we're making a HTTPS request to www.example.com with a custom HTML body containing a h1 tag. We set the headers to indicate that the body is in the HTML format. Finally, we send the request using the request() method with the HTTP method (POST), the API endpoint (/path/to/api), the custom HTML body, and the headers. We then get the response using the getresponse() method, and print the response status and body.

gistlibby LogSnag