Junior — Middle
How is a file sent via HTTP POST request?
sobes.tech AI
Answer from AI
To send a file via HTTP POST request in Python, the requests library is often used. The file is passed in the files parameter as a dictionary, where the key is the form field name, and the value is a tuple containing the filename and the file object.
Example:
import requests
url = 'https://example.com/upload'
files = {'file': ('example.txt', open('example.txt', 'rb'))}
response = requests.post(url, files=files)
print(response.status_code)
This request will send the file as part of multipart/form-data, which is typically required for uploading files to a server.