Tried million of things and only works from Postman and not from Python, any help?
I tried sending the video using BytesIO
buffer and it said the media isn't supported and tried using open(file) and said the same thing...
This is the code from Python to upload using open(took from Postman):
import base64
import requests
url = "https://{site}/wp-json/wp/v2/media" #not showing the site on purpose
username = "" #security concerns...
password = ""
data_sauth = username + ":" + password
token = base64.standard_b64encode(data_sauth.encode())
payload = {'title': 'test111'}
files=[
('file',('video123.mp4',open('/Users/{user}/Programming/video123.mp4','rb'),'application/octet-stream'))
]
headers = {
'Content-Disposition': 'form-data; filename="test123.mp4"',
'Content-Type': 'video/mp4',
"Authorization": "Basic " + token.decode("utf-8"),
# "Host": "www.{site}", #tried with and without, not working
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
if response.status_code == 201:
print("Post created successfully:" + response.text)
data = response.json()
print("-----------------------------------------------------------")
print(f"Link to post: {data['link']} \n ")
else:
print("Failed to create post: ", response.status_code, response.text)
The post is uploaded but the media isn't supported Image showing that it isn't being supported
This is how I tried to upload using Bytes IO on another occasion and got the same error:
file_info = await context.bot.get_file(postMediaID)
file_buffer = io.BytesIO()
await file_info.download_to_memory(file_buffer)
result = wp_connection.postWpMedia(
title=postTitle, media_content=file_buffer.getvalue(), status=postStatus
)
Reminding again that Postman did the job good, but I couldn't replicate it on Python.
Got a piece of code that says how to solve this: https://stackoverflow.com/a/52633086/8971211
requests.put(url, data=open(filename, 'rb'))
I noticed using "files" as documented in requests library prepends a bunch of garbage to the file. You inspect that with xxd