How To Upload A Binary/video File Using Python Http.client Put Method?
I am communicating with an API using HTTP.client in Python 3.6.2. In order to upload a file it requires a three stage process. I have managed to talk successfully using POST method
Solution 1:
uploadstep2.request("PUT", myUniqueUploadUrl, body="C:\Test.mp4", headers=headers)
will put the actual string "C:\Test.mp4"
in the body of your request, not the content of the file named "C:\Test.mp4"
as you expect.
You need to open the file, read it's content then pass it as body. Or to stream it, but AFAIK http.client
does not support that, and since your file seems to be a video, it is potentially huge and will use plenty of RAM for no good reason.
My suggestion would be to use requests
, which is a way better lib to do this kind of things:
import requests
withopen(r'C:\Test.mp4'), 'rb') as finput:
response = requests.put('https://grabyo-prod.s3-accelerate.amazonaws.com/youruploadpath', data=finput)
print(response.json())
Solution 2:
I do not know if it is useful for you, but you can try to send a POST request with requests module :
import requests
url = ""data = {'title':'metadata','timeDuration':120}
mp3_f = open('/path/your_file.mp3', 'rb')
files = {'messageFile': mp3_f}
req = requests.post(url, files=files, json=data)
print (req.status_code)
print (req.content)
Hope it helps .
Post a Comment for "How To Upload A Binary/video File Using Python Http.client Put Method?"