I have a request that succeeds in postman, but just fails when I call it from my JS code. The requests are similar in the Chrome dev tools, and I suspect that I'm just not adding the file correctly. This is how the file is added in the postman:
This is my JS code:
let formData = new FormData();
formData.append('file', event.target.files[0]);
fetch('http://localhost:8080/file/upload', {
method: 'POST',
headers:{
'Content-Type': 'multipart/form-data',
'Authorization': 'Bearer ' + JWT
},
body:formData
}).then(response => console.log(response));
This code is executed when the file of an input (type='file') changes. When I console.log the event.target.files[0], I get:
Am I adding the file wrong?
'Content-Type': 'multipart/form-data',
multipart/form-data
needs a parameter to describe the boundry marker.
fetch
will generate an appropriate Content-Type
header automatically, but you are overriding it and replacing it with a broken one.
Remove that line.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.