I’m trying to upload a file to a column using the monday api through node app. I took an example from github and modified the query to upload to a column. I get this error when I run the program.
{ error_message: ‘Unsupported query’, status_code: 400 }
I can’t tell what the issue is with the query to monday or if there’s another issue in the code. The code I used is posted below.
/// those are the main dependecies you’ll need to have installed in this example;
const fs = require(‘fs’);
const fetch = require(‘node-fetch’);
/// Replace with your actual API Key
const API_KEY= “XXXXXXXXXXXX”;
/// Notice the /file/ endpoint - only this endpoint will support variables in this type of call
const url = ‘https://api.monday.com/v2/file’;
/// This is your mutation query - can also be adapted to send files to file columns instead
const query = ‘mutation add_file($file: File!, $itemId: Int!) {add_file_to_column(item_id: $itemId, column_id: 'file', file: $file) {id}}’
/// These are the variables you are sending. Can be adapted to a call that would add the file to a file oclumn instead;
var vars = {“updateId”:1234};
///This is the mapping for the API call, where you specify that a variable should be considered as the variables[file] of the request.
///This can be called “image”, or simply file - as long as it matches the name of the multipart request later down the line.
var map = {“file”:“variables.file”};
/// this is the path to the file you’d like to upload to monday.com
var upfile = ./test.xlsx
;
var data = “”;
const boundary = “xxxxxxxxxxxxxxx”;
fs.readFile(upfile, function(err, content){
// simple catch error
if(err){
console.error(err);
}
//below, we will construct a multipart request. Take a look at the "name" within each part, as those will refer to different parts of the API call.
// construct query part
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"query\"; \r\n";
data += "Content-Type:application/json\r\n\r\n";
data += "\r\n" + query + "\r\n";
// construct variables part
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"variables\"; \r\n";
data += "Content-Type:application/json \r\n\r\n";
data += "\r\n" + JSON.stringify(vars) + "\r\n";
// construct map part
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"map\"; \r\n";
data += "Content-Type:application/json\r\n\r\n";
data += "\r\n" + JSON.stringify(map)+ "\r\n";
// construct file part - the name needs to be the same as passed in the map part of the request. So if your map is {"image":"variables.file"}, the name should be image.
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"file\"; filename=\"" + upfile + "\"\r\n";
data += "Content-Type:application/octet-stream\r\n\r\n";
var payload = Buffer.concat([
Buffer.from(data, "utf8"), // data has update id and mutation query
new Buffer.from(content, 'binary'),
Buffer.from("\r\n--" + boundary + "--\r\n", "utf8"),
]);
// construct request options
var options = {
method: 'post',
headers: {
"Content-Type": "multipart/form-data; boundary=" + boundary,
"Authorization" : API_KEY
},
body: payload,
};
// make request
fetch(url, options)
.then(res => res.json())
.then(json => console.log(json));
});