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!