make a http request with a custom body in python

Here is an example of making a POST request with a JSON body in Python using the requests library:

main.py
import requests
import json

url = "http://example.com/api"
data = {"key1": "value1", "key2": "value2"}
headers = {"content-type": "application/json"}

response = requests.post(url, data=json.dumps(data), headers=headers)

print(response.json())
246 chars
11 lines

This example assumes that you want to send a JSON body to the server. If you want to send a different type of body, you should adjust the headers accordingly.

related categories

gistlibby LogSnag