Cannot send Request using fetch


#1

I was trying to send a image using fetch api but i was getting status 400
fetch(‘https://api.uploadcare.com/files/?stored=true’, {
method: ‘POST’,
headers:{
“Content-Type”: “application/x-www-form-urlencoded”,
‘Accept’: ‘application/vnd.uploadcare-v0.5+json’,
‘Authorization’: ‘Uploadcare.Simple public_key:private_key’ },
body: data
})


#2

Hi @varun1010.meka,

If by “send an image” you mean “upload an image”, you need to use Upload API instead of REST API. For example

const data = new FormData();
data.set('UPLOADCARE_PUB_KEY', 'your_public_key')
data.set('UPLOADCARE_STORE', '1')
data.append('file', file);
fetch('https://upload.uploadcare.com/base/', {
    method: 'POST',
    body: data
}).then(res => res.json())  
  .then(data => {
    console.log(res.data);
}).catch(error => {
    console.log(error);
});