I am trying to send a file to a Java API endpoint from a JavaScript application. The Java endpoint accepts a binary string for the file in documentation. In the Java code it is accepting a MultipartFile. I’ve tried a few things to convert the JavaScript Base64 encoded file string, but still receiving “415: Unsupported Media Type”.
Here is my recent code attempt:
const handleFilesCallback = async (result) => {
const fileBase64String = result.value.content; // get Base64 encoded file
const byteCharacters = atob(fileBase64String); // convert to byte string
let ia = new Uint8Array(byteCharacters.length); // build Uint8Array
for (let i = 0; i < byteCharacters.length; i++) {
ia[i] = byteCharacters.charCodeAt(i);
}
const binaryString = new Blob(ia, { type: "" }); //create Blob
sendFile(binaryString);
};
Here is the API call:
const sendFile = async (file) => {
const response = await fetch(
"javaApiUrl",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
yourFile: file,
}),
}
).catch((error) => {
console.log("error:", error);
});
const responseJSON = await response.json();
return responseJSON;
};
How do I convert the Base64 encoded file string so the MultipartFile/binary string portion of the Java API is happy with what I send it?