I was making a python script that uploads local pdf files to my wordpress website, using the wordpress api, but for some reason, the post method with media endpoint just gives me a lot of random data including other media files instead of uploading my pdf.
wp_credentials = f'{wp_user}:{wp_password}'
token = base64.b64encode(wp_credentials.encode())
wp_header = {
'Authorization': 'Basic ' + token.decode('utf-8')
}
def upload_to_wordpress(file_path):
media = {
'file': open(file_path, 'rb'),
'caption': f'invoice: {file_path}',
'description': f'invoice: {file_path}'
}
pdf = requests.post(wp_url, headers=wp_header, files=media)
print(pdf)
pdf_url = str(json.loads(pdf.content))
print(pdf_url)
return pdf_url
`
I tried following the documentation and many other tutorials, but I stumble upon the same problem each time.
It also gives me status [200] insetead of the intended [201].
Doru Floare is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.