【发布时间】:2017-12-11 04:24:28
【问题描述】:
WordPress REST API 显然不允许我上传内容类型为“image/jpeg”的文件。
如果我将 content-type 设置为空字符串,我可以创建一个媒体项目并上传一个附件(该文件同时出现在媒体库和 wp_content/uploads 目录中)。但是,这样做会导致 WordPress 无法处理的无效图像。
这是我正在使用的代码:
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)
当我将 Content-Type 设置为 'image/jpg'、'image/jpeg'、'image'、'multipart'、'application' 或 'application/image'(我快绝望了)时,我得到了 403 .
如果我按照其他人的建议将其设置为“应用程序/json”,我会得到一个 400 并显示“通过了无效的 JSON 正文”。
如果我省略 Content-Type,我会得到 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),如果我能够上传 Content-Type = '' 的文件,建议的 PHP.ini 错误可能不适用。
有什么建议吗?
【问题讨论】:
标签: python wordpress rest api python-requests