如何使用python/django POST请求将图片上载到woocommerce

如何使用python/django POST请求将图片上载到woocommerce,python,django,wordpress,woocommerce,Python,Django,Wordpress,Woocommerce,我已经创建了一个woocommerce网页,我正在尝试使用Django/Python与我的网页同步。从文件中: 我正在使用,而且它似乎正在处理get请求,我找不到一种方法使它能够处理post请求 我经常遇到这样的错误: TypeError: <open file 'test.jpg', mode 'rb' at 0x104b4ced0> is not JSON serializable 更新:我已尝试使用来自本地主机的图像的确切路径,例如“http://localhost:8888

我已经创建了一个woocommerce网页,我正在尝试使用Django/Python与我的网页同步。从文件中:

我正在使用,而且它似乎正在处理get请求,我找不到一种方法使它能够处理post请求

我经常遇到这样的错误:

TypeError: <open file 'test.jpg', mode 'rb' at 0x104b4ced0> is not JSON serializable

更新:我已尝试使用来自本地主机的图像的确切路径,例如“
http://localhost:8888/wordpress/wp-content/uploads/2016/04/test.jpg
“在浏览器上运行正常的地方,我可以看到图片。当我在post请求中使用此路径时,它会产生相同的错误。我还尝试使用相对路径,例如“
file:///Users/tinyOS/Sites/wordpress/wp-content/uploads/2016/04/test.jpg
“仍然是相同的错误代码。

因此我设法找到了问题的解决方案。以防将来有人需要它

为了能够将图片上传到woocommerce,您需要有一个有效的url路径(例如
http://localhost:8888/wordpress/wp-content/uploads/2016/04/test.jpg

为了获得该url,您需要先上传一个文件到woocommerce,其中包含相对路径,然后作为第二步,检索路径并将其添加到第二个post请求中,其中包含要发布的产品的所有数据

python的工具是。我还发现手册中包含了比文档更有用的更多分析示例:

下面的示例演示了上载图像的过程。代码取自手册:

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts

client = Client('http://mysite.wordpress.com/xmlrpc.php', 'username', 'password')

# set to the path to your file
filename = '/path/to/my/picture.jpg'

# prepare metadata
data = {
'name': 'picture.jpg',
'type': 'image/jpeg', # mimetype
}

# read the binary file and let the XMLRPC library encode it into base64
with open(filename, 'rb') as img:
    data['bits'] = xmlrpc_client.Binary(img.read())

response = client.call(media.UploadFile(data))

# response == {
# 'id': 6,
# 'file': 'picture.jpg'
# 'url': 'http://www.example.com/wp-content/uploads/2012/04/16/picture.jpg',
# 'type': 'image/jpeg',
# }
attachment_id = response['id']
作为第二步,您可以创建一个函数,将所有信息发布到您的woocommerce商店。从中获取的代码示例。您只需创建一个包含所有数据的字典:

data = {
    "product": {
        "title": "Premium Quality",
        "type": "simple",
        "regular_price": "21.99",
        "description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
        "short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
        "categories": [
            9,
            14
        ],
        "images": [
            {
                "src": "http://example.com/wp-content/uploads/2015/01/premium-quality-front.jpg",
                "position": 0
            },
            {
                "src": "http://example.com/wp-content/uploads/2015/01/premium-quality-back.jpg",
                "position": 1
            }
        ]
    }
}

print(wcapi.post("products", data).json())
src:
需要替换为从上传请求中检索到的url,瞧。如果你知道要使用哪些工具,那就很简单;如果你不知道,那就很复杂


我希望这能有所帮助。

所以我设法找到了解决问题的办法。以防将来有人需要它

为了能够将图片上传到woocommerce,您需要有一个有效的url路径(例如
http://localhost:8888/wordpress/wp-content/uploads/2016/04/test.jpg

为了获得该url,您需要先上传一个文件到woocommerce,其中包含相对路径,然后作为第二步,检索路径并将其添加到第二个post请求中,其中包含要发布的产品的所有数据

python的工具是。我还发现手册中包含了比文档更有用的更多分析示例:

下面的示例演示了上载图像的过程。代码取自手册:

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts

client = Client('http://mysite.wordpress.com/xmlrpc.php', 'username', 'password')

# set to the path to your file
filename = '/path/to/my/picture.jpg'

# prepare metadata
data = {
'name': 'picture.jpg',
'type': 'image/jpeg', # mimetype
}

# read the binary file and let the XMLRPC library encode it into base64
with open(filename, 'rb') as img:
    data['bits'] = xmlrpc_client.Binary(img.read())

response = client.call(media.UploadFile(data))

# response == {
# 'id': 6,
# 'file': 'picture.jpg'
# 'url': 'http://www.example.com/wp-content/uploads/2012/04/16/picture.jpg',
# 'type': 'image/jpeg',
# }
attachment_id = response['id']
作为第二步,您可以创建一个函数,将所有信息发布到您的woocommerce商店。从中获取的代码示例。您只需创建一个包含所有数据的字典:

data = {
    "product": {
        "title": "Premium Quality",
        "type": "simple",
        "regular_price": "21.99",
        "description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
        "short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
        "categories": [
            9,
            14
        ],
        "images": [
            {
                "src": "http://example.com/wp-content/uploads/2015/01/premium-quality-front.jpg",
                "position": 0
            },
            {
                "src": "http://example.com/wp-content/uploads/2015/01/premium-quality-back.jpg",
                "position": 1
            }
        ]
    }
}

print(wcapi.post("products", data).json())
src:
需要替换为从上传请求中检索到的url,瞧。如果你知道要使用哪些工具,那就很简单;如果你不知道,那就很复杂

我希望这有帮助