如何使用Python Requests将图片发送到Strapi?

3
我试图使用Python Requests将图像/文件发送到Strapi集合类型?
我有一个名为“log”的集合类型,其中包含一个媒体(和两个文本字段)。我不知道如何创建一个带有图像的新的“log”。
我只是在编写代码,但目前我有以下内容(我正在尝试使图像可流式传输,希望这样可以解决问题):
import requests
from utils.networking import LOCAL, PORT

import io
import numpy as np
import matplotlib.pyplot as plt

# I converted the image from numpy array to png
buf = io.BytesIO()
plt.imsave(buf, cvimage, format='png')
image = buf.getvalue()

payload = {
    "Type": 'info'
    "Message": 'Testing',
}

req = requests.post(f'http://localhost:1337/logs', json=payload, data=image)

我曾试过使用requests.postfiles参数代替data,但是无法正常工作。另外,我也尝试过向/upload发送请求,但失败了。
3个回答

6

终于做到了...

重点是,您必须先将图片/文件上传到/upload。然后,在媒体字段中引用刚刚上传的id,将媒体添加到集合类型条目中。

像这样上传媒体:

import requests
import json

files = {'files': ('Screenshot_5.png', open('test.jpeg', 'rb'), 'image', {'uri': ''})}
response = requests.post('http://localhost:1337/upload', files=files)

print(response.status_code)
# `response.text` holds the id of what you just uploaded

现在你的媒体文件应该已经在 Strapi 媒体库中了(建议再次确认)。

最后,现在你可以像平常一样创建一个条目,并使用所上传文件的 id 来添加媒体文件。

payload = {
    "Type": 'info',
    "Message": 'lorem ipsum beep bop',
    "Screenshot": 1, # this is the id of the media you uploaded
}
response = requests.post('http://localhost:1337/logs', json=payload)

print(response.status_code)

我知道在文件上传请求中,“image”是MIME类型,但“{'uri': ''}”这个对象是什么? - Gers
有没有一种方法可以在创建对象时附加媒体? - Steve Scott

1

首先,您应该将文件发布到/upload/端点,您的请求主体必须是一个form-data,例如在postman中具有以下值:

KEY : files 
VALUE : "The file you want to save"

请注意,KEY值必须始终为files,然后响应将类似于这样:

[
    {
        "_id": "5f38db271f720e3348b75327",
        "name": "testImage",
        "alternativeText": null,
        "caption": null,
        "hash": "testImage_d12913636e",
        "ext": ".jpeg",
        "mime": "image/jpeg",
        "size": 18.4,
        "width": 512,
        "height": 213,
        "url": "/uploads/testImage_d12913636e.jpeg",
        "formats": {
            "thumbnail": {
                "hash": "thumbnail_testImage_d12913636e",
                "ext": ".jpeg",
                "mime": "image/jpeg",
                "width": 245,
                "height": 102,
                "size": 5.03,
                "path": null,
                "url": "/uploads/thumbnail_testImage_d12913636e.jpeg"
            },
            "small": {
                "hash": "small_testImage_d12913636e",
                "ext": ".jpeg",
                "mime": "image/jpeg",
                "width": 500,
                "height": 208,
                "size": 17.03,
                "path": null,
                "url": "/uploads/small_testImage_d12913636e.jpeg"
            }
        },
        "provider": "local",
        "related": [],
        "createdAt": "2020-08-16T07:07:19.355Z",
        "updatedAt": "2020-08-16T07:07:19.355Z",
        "__v": 0,
        "id": "5f38db271f720e3348b75327"
    }
]

然后,每当您想要将图像或文件设置为字段时,只需使用上述响应的id即可。


-1
在 Strapi 3 版本中,按照截图中的方式编写 Postman 请求,并转到代码部分以获取 Postman 中自动生成的代码。 postman request screenshot

你好!欢迎来到StackOverflow。这个问题是关于使用Python的´request´库生成请求,而你的答案并没有提供帮助。 - Joël

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接