Hi i am looking for a way to create a post to a 3rd party api which require a multipart/form-data
The body i have to send is composed by a json payload and a file wix is stored in wix media or as an item in the wix database.Here is my code. Any idea?
export async function uploadFile() {
const basicUrl="https://sandbox.esignlive.com/api"
var requestURL = basicUrl + "/packages";
console.log("starting function")
// convert Wix URL to https:
// => You will need to write the function
// convert the file to readable stream
let fileUrl = "public url of my file in wix media";
var blobRes = await fetch(fileUrl)
var blob = await blobRes;
//console.log(blob)
let toSend = {
payload: JSON.stringify({
"name": "Example Transaction: ",
"description": "Created using SWAGGER and TextTags",
"type": "PACKAGE",
....
}),
file: blob
}
console.log({toSend});
let payload = getFormData(toSend);
let res = await fetch(requestURL , {
method:"POST" ,
headers: {
'Content-Type': "multipart/form-data"
},
body: toSend
})
console.log({res})
let json = res.json();
return json;
}
function getFormData(data) {
if(data === undefined) return;
let {something, file} = data;
let formdata = new FormData();
console.log("created")
formdata.append("file", file);
formdata.append("payload", something);
return formdata;
}