I'm trying to construct a simple Cloudinary image upload based on this codepen example: https://codepen.io/team/Cloudinary/pen/QgpyOK --
I've converted it to work with fetch
but even though I've gone to my Cloudinary settings and created an unsigned upload preset, which I'm providing to the headers, I'm still getting an error
POST https://api.cloudinary.com/v1_1/myproject/upload 400 (Bad Request)
with the error message
Upload preset must be specified when using unsigned upload
The code I'm using is the following
_callCloudinaryApi(file, method = 'post') {
const config = {
method
}
const cloudName = "myproject" // fictional project name here
const unsignedUploadPreset = "mypreset" // fictional preset name here
const url = `https://api.cloudinary.com/v1_1/${cloudName}/upload`;
const fd = new FormData();
fd.append('upload_preset', unsignedUploadPreset);
fd.append('tags', 'browser_upload'); // Optional - add tag for image admin in Cloudinary
fd.append('file', file);
if (method !== 'get') {
config.headers = {}
config.headers['X-Requested-With'] = 'XMLHttpRequest'
}
return fetch(url, config)
.then(res => res.json())
.then(res => {
console.log(res)
return res;
})
}
Can anyone help me to determine what the problem might be? Thanks very much!