0

I'm trying to upload audio files to a Wordpress site via the API. I'm using the requests_toolbelt for the multipart encoding. Here is my code:

import new_auth
import requests_toolbelt
import pathlib

class AudioFile:
    def __init__(self, File):
        self.InputFile = File
        self.Auth=new_auth.auth
        self.Url = "https://my-website-/wp-json/wp/v2/media"

    def upload_multipart(self):
        fields ={}
        path = pathlib.Path(self.InputFile)
        total_size = path.stat().st_size
        filename = path.name

        with open(self.InputFile, "rb") as f:
            fields["file"] = ("filename", f, 'audio/mpeg')
            m = requests_toolbelt.MultipartEncoder(fields=fields)
            headers = {'content-type': m.content_type}
            self.Req = requests.post(self.Url, data=m, headers=headers, auth=self.Auth)
            print (self.Req.status_code)
            print (self.Req.text)



ToUpload = AudioFile("UploadTest.mp3")
ToUpload.upload_multipart()


This is the error I'm getting on the server side:

Got error 'PHP message: PHP Warning:  Missing boundary in multipart/form-data POST data in Unknown on line 0'

If somebody could tell me what I'm doing wrong, I'd appreciate it!

user1432738
  • 101
  • 1
  • 5
  • this seems like a possible duplicate of https://stackoverflow.com/questions/22567306/how-to-upload-file-with-python-requests – Joran Beasley Apr 05 '23 at 04:57
  • basically if you want to upload files just use `requests.post(files=...)` (that should also take care of the headers i think) – Joran Beasley Apr 05 '23 at 04:58
  • Does uploading using the files keyword put the whole file in memory? Also, I was trying to have a progress bar, which I can do with requests_toolbelt, but I don't think you can do that with the requests files keyword. – user1432738 Apr 06 '23 at 16:25

0 Answers0