Hello!
I am trying to upload an image via API Create Assets but all the time I get this page
https://s3.amazonaws.com/webflow-prod-assets/6565390dbfdb733f670ef03b/662102f927cd62302721c4b9_2013-Hyundai-H-1-25-CRDi-VGT-Wagon-Auto%20(1).webp
My code looks like this (copied from API docs; key and site-id is hidden):
const url = ‘https://api.webflow.com/v2/sites/{site-id}/assets’;
const options = {
method: ‘POST’,
headers: {
accept: ‘application/json’,
‘content-type’: ‘application/json’,
authorization: ‘Bearer {key}’
},
body: JSON.stringify({fileName: ‘2013-Hyundai-H-1-25-CRDi-VGT-Wagon-Auto (1).webp’, fileHash: ‘38fa8ae5f3bea9d2d5f16e43ede26c17a4c77639f135a0ef5f2691ae5a641b88’})
};
let uploadDetails = await fetch(url, options)
.then(res => res.json())
.catch(err => console.error(‘error:’ + err));
// You’ll recieve a body of Upload Details which has th following key information needed to upload the asset
async function uploadImageToS3(uploadDetails, imagePath) {
const s3Endpoint = uploadDetails.uploadUrl;
// Create a new FormData object to send as a multipart/form-data request
const form = new FormData();
// Add form fields
for (const key in uploadDetails.uploadDetails) {
form.append(key, uploadDetails.uploadDetails[key]);
}
// Append the actual asset file to the FormData object
form.append(‘file’, fs.createReadStream(imagePath));
try {
const response = await axios.post(s3Endpoint, form, {
headers: {
…form.getHeaders(),
},
});
if (response.status === 201) {
console.log('Upload successful.');
} else {
console.error('Upload failed with status:', response.status);
}
} catch (error) {
console.error(‘Error:’, error);
}
}
const imagePath = ‘https://5b20d393476ca24df5d861a6c2eb270d.cdn.bubble.io/f1713439389865x198083192425617900/2013-Hyundai-H-1-25-CRDi-VGT-Wagon-Auto%20(1).webp’; // Replace with the actual path to your image file
uploadImageToS3(uploadDetails, imagePath);
What am I doing wrong?