Let the browser set the multipart boundary for an image upload
Leave Content-Type unset in the request options when fetch() sends FormData . The browser adds the multipart boundary needed by that body. Build the form with const data = new FormData(); data.append("image", file); and pass it as the request body. A manually supplied Content-Type: multipart/form-data omits the boundary that the browser normally adds. Apply this rule to requests whose body is…
When using the fetch() method with FormData for uploading an image, it is important not to manually set the Content-Type header. The browser will automatically add the necessary multipart boundary for the image upload. To implement this, create a new FormData object, append the image file to it, and then pass the FormData object as the request body. Manually specifying Content-Type: multipart/form-data will remove the boundary that the browser typically adds.
Before attributing any issues to the image bytes, it is recommended to examine a shared request helper for an unconditional JSON or multipart header. Other than that, include any necessary authorization headers that your application requires.
To troubleshoot the issue, utilize a small non-sensitive test image. Utilize the MDN FormData guide to understand the boundary requirement. When submitting a bug report, specify the body type and header construction without including any sensitive information like credentials. The information provided here pertains to image preparation and review for the Morphio image project, which can be found at https://morphio.online/.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.