DEV Community

Lê Vũ Huy
Lê Vũ Huy

Posted on

Upload image to an entry on Strapi

Strapi is great but their document is really confusing. I spent about 2 hours trying to achieve this. When you create a new entry from REST API, you may also want to upload image to that entry.

1. REST API

Try make a POST request to

https://STRAPI_URL/api/upload
Enter fullscreen mode Exit fullscreen mode

with the form-data:

ref: the table ID (ex: `api::submission.submission`)
refId: the entry ID
field: the field to upload
files: the file to upload
Enter fullscreen mode Exit fullscreen mode

This is how we do it in Postman
Postman

2. Javascript SDK

strapi-js-sdk haven't supported upload file. Luckily, we can do it with a workaround.

const formData = new FormData();
formData.append(
    "files",
    blobFile,
    "file-name.jpg"
);
formData.append("ref", "api::submission.submission");
formData.append("refId", entryId);
formData.append("field", "speakingAudio");

const response = await strapi.request("POST", "/upload", {
    headers: {
        "Content-Type": "multipart/form-data",
    },
    data: formData,
});
Enter fullscreen mode Exit fullscreen mode

If you have a question, don't hesitate to comment below.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post →

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay