Skip to content

Instantly share code, notes, and snippets.

@joao-pedrozo
Created June 27, 2022 14:01
Show Gist options
  • Save joao-pedrozo/545076b4867311dd99a7b1b4b3aba353 to your computer and use it in GitHub Desktop.
Save joao-pedrozo/545076b4867311dd99a7b1b4b3aba353 to your computer and use it in GitHub Desktop.
Upload image to strapi
try {
const formData = new FormData();
const { data: stream } = await axios.get(
"https://images.unsplash.com/photo-1611095785020-1ba3dd228ea7?ixid=MXwxMjA3fDF8MHxlZGl0b3JpYWwtZmVlZHwxfHx8ZW58MHx8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=900&q=60",
{ responseType: "stream" }
);
formData.append("files", stream);
formData.append("ref", "api::article.article"); // Collection name
formData.append("refId", 10); // Item ID
formData.append("field", "thumbnail"); // Field Name
await axios.post(`${API_URL}/upload`, formData, {
headers: {
"Content-Type": "multipart/form-data",
Authorization: `Bearer ${API_TOKEN}`,
},
});
} catch (err) {
console.log(err.response.data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment