我错过了一些东西。几天来,我一直在编写一个 python 脚本来更新 woocommerce 中的产品,但它无法更新并且没有错误或响应。我有 REST API 日志插件来检查发生了什么,这是 ist 记录:
请求标头
{
"content_length": "1218",
"content_type": "application\/json;charset=utf-8",
"connection": "keep-alive",
"accept": "application\/json",
"accept_encoding": "gzip, deflate",
"user_agent": "WooCommerce-Python-REST-API\/3.0.0",
"host": "www.example.com",
"authorization": "Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
查询参数
[]
机身参数
[]
正文内容
"{"update": [{"id": 8056, "manage_stock": "true", "stock_quantity": 5, "name": "Product_1", "status": "publish", "regular_price": "12.95", "categories": [{"id": 21485}]}, {"id": 44848, "manage_stock": "true", "stock_quantity": 48, "name": "Product_2", "status": "publish", "regular_price": "0.3", "categories": [{"id": 21485}]}, {"id": 48978, "manage_stock": "true", "stock_quantity": 42, "name": "Product_3", "status": "publish", "regular_price": "1.1", "categories": [{"id": 21485}]}, {"id": 8062, "manage_stock": "true", "stock_quantity": 1, "name": "Product_4", "status": "publish", "regular_price": "5.0", "categories": [{"id": 21482}]}, {"id": 21588, "manage_stock": "true", "stock_quantity": 9, "name": "Product_5", "status": "publish", "regular_price": "1.8", "categories": [{"id": 21477}]}, {"id": 49676, "manage_stock": "true", "stock_quantity": 15, "name": "T-Shirt 100% Love", "status": "publish", "regular_price": "8.9", "categories": [{"id": 21484}]}]}"
响应标头
{
"Set-Cookie": "wfwaf-authcookie-36dbd1f8f1d69da955e78f5a9261aa9f=1%7Cadministrator%7Cmanage_options%2Cunfiltered_html%2Cedit_others_posts%2Cupload_files%2Cpublish_posts%2Cedit_posts%2Cread%7C3c79ea23ac438c0347855b3b416a54ed3518ed76b6660972e35663a7dd56841f; expires=Tue, 13-Feb-2024 204636 GMT; Max-Age=43200; path=\/; secure; HttpOnly",
"Content-Type": "application\/json; charset=UTF-8",
"X-Robots-Tag": "noindex",
"Link": "<https\/\/example.com\/wp-json\/>; rel="https\/\/api.w.org\/"",
"X-Content-Type-Options": "nosniff",
"Access-Control-Expose-Headers": "X-WP-Total, X-WP-TotalPages, Link",
"Access-Control-Allow-Headers": "Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type",
"Allow": "POST, PUT, PATCH",
"Expires": "Wed, 11 Jan 1984 050000 GMT",
"Cache-Control": "no-cache, must-revalidate, max-age=0, no-store, private"
}
响应正文
{
"data": [],
"headers": {
"Allow": "POST, PUT, PATCH"
},
"status": 200
}
我的代码如下所示:
l = len(product_list)
str_start = "{\"update\": ["
str_end = "]}"
batch_size = 50
for i in range(0, l, batch_size):
one_product_batch = product_list[i:i + batch_size]
d = ", ".join(str(n) for n in one_product_batch)
data = str_start + d + str_end
print("_________________________BATCH", i, "_________________________")
response = wcapi.post("products/batch", data).json()
print(response)
所以...我对 JSON 的理解更好了一点,不。这是发送前列表的样子:
product_list = [
{
'id': 21750,
'manage_stock': 'true',
'stock_quantity': 88,
'name': 'PRODUCT_1',
'status': 'publish',
'regular_price': '0.3',
'categories': [{'id': 21480}]},
{
'id': 6674,
'manage_stock': 'true',
'stock_quantity': 72,
'name': 'PRODUCT_2',
'status': 'publish',
'regular_price': '1.1',
'categories': [{'id': 21475}]
}
]
这是将其批量发送到 WooCommerce 的方法:
l = len(product_list)
print("Lenght: ", l)
batch_size = 100
for i in range(0, l, batch_size):
one_product_batch = product_list[i:i + batch_size]
data = {'update': one_product_batch}
# print(one_product_batch)
print("_________________________BATCH", i, "_________________________")
response = wcapi.post("products/batch", data).json()
print(response)