使用 Python 向 API Prestashop 发送 POST 请求

Posted

技术标签:

【中文标题】使用 Python 向 API Prestashop 发送 POST 请求【英文标题】:POST request to API Prestashop with Python 【发布时间】:2019-10-19 04:42:57 【问题描述】:

我通过Prestashop API实现列出和创建产品。我想在我的网站上自动化一点产品更新过程。

但是我在尝试使用图像创建新产品和将图像上传到我通过网络服务创建的产品时都遇到了问题。

我没有在我的代码中看到任何错误,所以我想知道我是否在使用 Prestashop API 时出错了。

我的代码:

def addNewImage(product_id):
   file = 'foto.png'
   fd = io.open(file, "rb")
   data = fd.read()
   r = requests.post(urlimg + product_id, data=data,auth=(key,""), headers='Content-Type': 'multipart/form-data')
   print(r.status_code)
   print(r.headers)
   print(r.content)

Prints:

500
'Server': 'nginx', 'Date': 'Fri, 31 May 2019 09:18:27 GMT', 'Content-Type': 'text/xml;charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Access-Time': '1559294307', 'X-Powered-By': 'PrestaShop Webservice', 'PSWS-Version': '1.7.5.2', 'Execution-Time': '0.003', 'Set-Cookie': 'PrestaShop-30ff13c7718a401c862ad41ea4c0505f=def50200b7a8c608f3053d32136569a34c897c09cea1230b5f8a0aee977e6caac3e22bea39c63c30bfc955fe344d2cbabf640dc75039c63b33c88c5f33e6b01f2b282047bfb0e05c8f8eb7af08f2cc5b0c906d2060f92fea65f73ce063bf6d87bd8ac4d03d1f9fc0d7b6bf56b1eb152575ef559d95f89fc4f0090124630ae292633b4e08cfee38cee533eb8abe151a7d9c47ed84366a5dd0e241242b809300f84b9bb2; expires=Thu, 20-Jun-2019 09:18:27 GMT; Max-Age=1728000; path=/; domain=example.com; secure; HttpOnly', 'Vary': 'Authorization', 'MS-Author-Via': 'DAV'

b'<?xml version="1.0" encoding="UTF-8"?>
\n<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
   \n<errors>
        \n<error>
            \n<code><![CDATA[66]]></code>
            \n<message><![CDATA[Unable to save this image]]></message>
        \n</error>
    \n</errors>
\n</prestashop>\n'

我尝试使用 python 的日志库,但只告诉我这个:

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): midominio:443

DEBUG:urllib3.connectionpool:https://midominio:443 "POST /api/images/products/20 HTTP/1.1" 500 None

我还尝试将文件 config/defines.inc.php 更改为我在 prestashop 论坛中阅读到的主动调试模式,但有任何区别。

我还探测了库 prestapyt(和 prestapyt3),但不适用于 python 3,并且我读到了与 presta 1.7 不兼容的内容

编辑: Display_errors 和 log_errors 在我的 Plesk 面板中被激活:

但是当我去 var/www/vhosts/midominio/logs/error_log

我在任何行中都看不到任何引用 php 的错误或 500 错误。

提前感谢您的任何建议...

编辑:我调查了建议作为回应,但返回相同的错误。

【问题讨论】:

你能在 php.ini 中打开 log_errors 并检查你的 PHP 错误日志,看看是什么触发了 500 错误吗? 【参考方案1】:

我认为问题出在 post 命令中,如果其他一切都在后端正常工作的话。 data 用于发送表单数据和其他文本数据。要上传文件,您应该这样做:

files = 'media': open('test.jpg', 'rb')
requests.post(url, files=files)

所以您的代码转换为:

def addNewImage(product_id):
   file = 'foto.png'
   fd = io.open(file, "rb")
   r = requests.post(urlimg + product_id, auth=(key,""), headers='Content-Type': 'multipart/form-data', files= 'media' : fd )
   print(r.status_code)
   print(r.headers)
   print(r.content)

【讨论】:

@Peisou 谷歌搜索后发现:medium.com/@altin/… 感谢您的建议,我试了一下,但我要保存的图像是一个大小为 32kb 的 png .. 更改它,但同样的错误。 我确信这是一个愚蠢的问题,但是如果您尝试以 .png 以外的格式保存会发生什么? 我也尝试使用 jpg。抱歉@jeremiah 我没有看到消息

以上是关于使用 Python 向 API Prestashop 发送 POST 请求的主要内容,如果未能解决你的问题,请参考以下文章

BigQuery:使用 python BQ API 向现有表添加新列

Python / C-Api:向模块添加类

如何使用 Python 向 Viber 机器人发送消息?

python Kaltura API Python脚本,用于向帐户中的所有条目添加标签

使用Mock进行第三方API的Django / Python测试

python ### send-event.py:通过REST API向OpenNMS发送事件。 ###