I want to know what the difference is between using the Fetch API and the Axios API specifically when making a PUT call to upload an image.
I spent the day trying different ways of sending the body (an image) of my fetch request to be uploaded to an S3 signed url, however the image uploaded to the S3 bucket kept showing the file to have 0 or 2 bytes.
When comparing the headers between both requests, I noticed the once difference was the Content-Length header. The Content-Length header value was 0 or 2 when using fetch and it was 477386 (depends on the size of the file) when using Axios.
// Fetch
const response = await api({
url: signedURL,
method: 'PUT',
body: image,
headers: {
'Content-Type': image.type,
},
})
// Axios
const response = await axios.put(signedURL, image, {
headers: {
'Content-Type': image.type,
},
})
I've tried several different ways of sending the body using fetch such as using a FormData object, using a Blob to send the image, and even just sending the image file alone (as shown above). I've also tried using Postman to test my upload and it worked without an issue.
Is there something I'm missing when using fetch that I need to do in order to have the correct Content-Length header?