How to send image to Strapi with Python Requests?

Viewed 847

I'm trying to send image / files to Strapi collection type with Python Requests?

I have a collection type called log which has a single media (and two text fields). I don't know how to create a new log with an image.

I'm just mashing code, but this is what I have at the moment (I'm trying to make the image streamable hoping that would work):

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)

I've tried using requests.post's files parameter instead of data, but I couldn't get that working. Also, I've tried posting to /upload as well, but failed.

3 Answers

Finally did it...

The main point is, you have to upload the images / files to /upload first. Then, to add the media to a collection type entry, in the media field, reference the id of what you just uploaded.

Upload the media like this:

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

Your media should be in the Strapi Media Library now (you should double-check this).

Finally, now you can create a entry (as you normally would) and use the id of what you uploaded to add media.

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)

First you should post your file to the /upload/ endpoint, your body must be a form-data with the following values in postman for example:

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

be careful, the KEY value must always be files,then the response will be something like this:

[
    {
        "_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"
    }
]

Then whenever you want to set the image or file to a field, you just need to use the id of the above response.

In Strapi version 3 write postman request as in screenshot and go to code section for auto generated code in postman postman request screenshot

Related