I would like users to be able to upload a file (i.e. an image) and have it sent to a 3rd party API for processing.
I’m using the form-data module. Unfortunately, it’s not as straight forward in Wix.
I have this function in the backend:
import {fetch} from 'wix-fetch';
import { mediaManager } from 'wix-media-backend';
const FormData = require("form-data");
export function APIRequest(file) {
let form = new FormData();
form.append("api_key", JSON.stringify({
'ApiKey' : 'users_api_key',
}));
form.append("file", file);
return fetch("3rd_party_api_endpoint", {
method: "POST",
body : form,
})
.then( (httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
} else {
return Promise.reject("Fetch did not succeed");
}
})
}
It does not work when:
-
Using the Wix provided upload buttons
-
I’m using custom javascript in an HTML frame then sending the result to the front Wix developer portion which then communicates to the backend
-
Trying to send the file as is
It only seems to work when I convert the file to a string and insert the string in the form:
form.append("file", JSON.stringify({
'file_string' : file_string, // File has been converted to a string
}));
Questions
Is there a size limit on outgoing Wix requests?
Is there a cleaner way to do this?