Issue
I have an example of curl request:
curl -X "POST" "http://192.168.24.62:8080/upload" \
-H 'Content-Type: multipart/form-data; charset=utf-8' \
-F "[email protected]" \
-F "filename=buffer.wav"
And it works well. I want to convert this request using requests
library in python. Here is my try:
import requests
filename = 'test.wav'
url = 'http://192.168.24.62:8080/upload'
with open(filename, 'rb') as file:
files = {'file': ('buffer.wav', file, 'audio/wav')}
headers = {'Content-Type': 'multipart/form-data; charset=utf-8'}
response = requests.post(url, headers=headers, files=files)
print(response.json())
I get an error:
requests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionResetError(54, 'Connection reset by peer')", ConnectionResetError(54, 'Connection reset by peer'))
What's wrong here?
Solution
The following is working on my machine (different IP:port obviously).
import requests
url = "http://192.168.24.62:8080/upload"
with open('test.wav', 'rb') as f:
files = {
'file': (f.name, f, 'audio/wav'),
'filename': ('', 'buffer.wav')
}
response = requests.post(url, files=files)
print(response.status_code)
print(response.text)
Answered By - alec_djinn Answer Checked By - Mary Flores (WPSolving Volunteer)