Python wordpressrestapi:Can';t上传内容类型图像/jpeg
WordPressRESTAPI显然不允许我上传内容类型为“image/jpeg”的文件 如果我将内容类型设置为空字符串,我可以创建一个媒体项并上载一个附加文件(该文件同时出现在媒体库和wp_content/uploads目录中)。然而,这样做会导致WordPress无法处理的无效图像 这是我正在使用的代码:Python wordpressrestapi:Can';t上传内容类型图像/jpeg,python,wordpress,rest,api,python-requests,Python,Wordpress,Rest,Api,Python Requests,WordPressRESTAPI显然不允许我上传内容类型为“image/jpeg”的文件 如果我将内容类型设置为空字符串,我可以创建一个媒体项并上载一个附加文件(该文件同时出现在媒体库和wp_content/uploads目录中)。然而,这样做会导致WordPress无法处理的无效图像 这是我正在使用的代码: def post(): url = 'https://www.example.com/wp-json/wp/v2/media' data = open('C:\\Pictu
def post():
url = 'https://www.example.com/wp-json/wp/v2/media'
data = open('C:\\Pictures\\foo.jpg', 'rb').read()
r = requests.post(url=url,
files={'filename1': data},
data={'title': 'Files test'},
headers={'Content-Type': '', 'Content-Disposition': 'attachment; filename={}'.format('foo.jpg')},
auth=('username', 'password'))
return (r.status_code, r.text)
当我将内容类型设置为‘image/jpg’、‘image/jpeg’、‘image’、‘multipart’、‘application’或‘application/image’(我绝望了)时,我得到了403
如果我按照其他人的建议将其设置为“application/json”,则会得到一个400,其中“传递的json正文无效”
如果我省略了内容类型,我会得到一个带有以下消息的500:
{"code":"rest_upload_unknown_error","message":"File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.","data": {"status":500}}')
我的文件不是空的(26kB),如果我能够上载内容类型为“”的文件,则建议的PHP.ini错误可能无法应用
有什么建议吗?好的,我有一个方法可以正确上传图像(只需上传数据而不是文件): 请注意,内容类型仍然设置为“”,但不知何故,它现在可以正常工作
def post():
url = 'https://www.example.com/wp-json/wp/v2/media'
data = open('C:\\Pictures\\foo.jpg', 'rb').read()
r = requests.post(url=url,
data=data,
headers={'Content-Type': '', 'Content-Disposition': 'attachment; filename={}'.format('foo.jpg')},
auth=('username', 'password'))
return (r.status_code, r.text)