Hi I’m trying to upload a file specifically images using the monday.com api mutation
mutation { add_file_to_column (item_id: 1234567890, column_id: "files", file: YOUR_FILE) { id } }
Which works great when using it based on this other comment by Matias to another post:
It works fine with files like pdf, docs, txt, xls, csv but whenever I try to do it with an image file (png, jpeg, webp etc.) it doesn’t work. It only gives me this error:
{
error_code: 'RecordInvalidException',
status_code: 422,
error_message: 'Validation failed: Resource Paperclip::Errors::NotIdentifiedByImageMagickError, Resource Paperclip::Errors::NotIdentifiedByImageMagickError, Resource Paperclip::Errors::NotIdentifiedByImageMagickError, Resource Paperclip::Errors::NotIdentifiedByImageMagickError, Resource Paperclip::Errors::NotIdentifiedByImageMagickError, Resource Paperclip::Errors::NotIdentifiedByImageMagickError',
error_data: {
error_data: 'Validation failed: Resource Paperclip::Errors::NotIdentifiedByImageMagickError, Resource Paperclip::Errors::NotIdentifiedByImageMagickError, Resource Paperclip::Errors::NotIdentifiedByImageMagickError, Resource Paperclip::Errors::NotIdentifiedByImageMagickError, Resource Paperclip::Errors::NotIdentifiedByImageMagickError, Resource Paperclip::Errors::NotIdentifiedByImageMagickError'
}
}
I tried changing content-type in my code to image but it still didn’t work. Here’s a snippet of my code for reference:
// get file url
const sourceFileUrl = file.url;
// Download file from source URL
fetch(sourceFileUrl)
.then((response) => response.arrayBuffer())
.then((content) => {
const boundary = "xxxxxxxxxx";
let data = "";
// Construct GraphQL mutation
data += `--${boundary}\r\n`;
data +=
'Content-Disposition: form-data; name="query"; \r\n';
data += "Content-Type:application/json\r\n\r\n";
data += `\r\nmutation ($file: File!) { add_file_to_column (file: $file, item_id: ${itemId}, column_id: "${column.id}") { id } }\r\n`;
// Construct file part
data += `--${boundary}\r\n`;
data += `Content-Disposition: form-data; name="variables[file]"; filename="${fileName}"\r\n`;
data += "Content-Type:application/octet-stream\r\n\r\n";
const payload = Buffer.concat([
Buffer.from(data, "utf8"),
Buffer.from(content, "binary"),
Buffer.from(`\r\n--${boundary}--\r\n`, "utf8"),
]);
// Construct request options
const options = {
method: "post",
headers: {
"Content-Type": `multipart/form-data; boundary=${boundary}`,
Authorization: `Bearer ${token}`,
},
body: payload,
};
// Make request to Monday.com API
fetch("https://api.monday.com/v2/file", options)
.then((res) => res.json())
.then((json) => console.log(json))
.catch((error) =>
console.error(
`Error uploading file: ${error.message}`
)
);
})
.catch((error) =>
console.error(`Error downloading file: ${error.message}`)
);