Hi, I have been using http request in wix to fetch data, delete data and all that. But yeasterday I decided to learn POST method. So far it’s been working great but then it hit me, what if I want to upload files, how would that be possible?
I promise I’ve read through the upload() function, it seems to make sense but it’s not working
The request is simple, the http-body has an image decoded to a base64 string. Something like this:
/9j/4AAQSkZJRgABAQAA2ADYAAD/4QCARXhpZgAATU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUA…
Here’s my code:
(I have the buffer npm module installed)
import { Buffer } from 'buffer'
export function post_upload(request) {
let type;
let body
if (request.query) {
type = request.query.type
body = request.body
}
let options = {
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
}
}
var buffer = Buffer.from(unescape(body), 'base64');
if (buffer) {
return uploadImage(buffer)
.then(image => {
options.body = [{
"message": "success"
}]
console.log(options)
return ok(options)
})
.catch((err) => {
options.body = [{
"message": "error"
}]
console.log(options)
return serverError(options)
})
} else {
options.body = [{
"noData": "error"
}]
console.log(options)
return serverError(options)
}
}
export function uploadImage(buffer) {
return mediaManager.upload(
"/uploads/subfolder",
buffer,
"img_1797.jpg", {
"mediaOptions": {
"mimeType": "image/jpeg",
"mediaType": "image"
},
"metadataOptions": {
"isPrivate": false,
"isVisitorUpload": true,
"context": {
"someKey1": "someValue1",
"someKey2": "someValue2"
}
}
});
}
The request is sending sucessfully, I’m not getting any server errors (Sometimes I get status code 500 other than that nothing just happens). No Image is uploaded because I don’t see anything in the visitor uploads folder.
NOTE: I’m sending the request through Swiftui/Xcode
Please point out where I’m wrong,
Brendan Okey-iwobi