简体   繁体   中英

How to POST an MP4 using Python requests?

I am trying to upload an MP4 file, using requests.post. I'm not sure if it is due to the size of the file, as it is 3.2MB and I have seen people saying they have issues over 1.5MB. Any help is appreciated. Thanks

def post_video(URL):

files = {'file': open(file_path, 'rb')}
response = requests.post(URL, files=files)
return response

The response code should 204, however it is returning 500. The request works on Swagger UI

It won't let me comment,

The most likely cause is that you're not adding a header to your post that is required. I'd look at the REST API's documentation or even the Swagger UI and enter the developer console, watch the output and ensure you're adding everything needed.

Headers usually include the content type and sometimes include an authentication token, have you authenticated with the end point?

Here is an example of my proposition:

    def getresponse:
     userpass = b64encode(b"<username>:<password>").decode("ascii")
     headers = {'Content-type':'video/mp4', 'Authorization': 'Basic ' + userpass}
     files = {'file': open(file_path, 'rb')}
     response = requests.post(URL, files=files, headers=headers)
     return response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM