Hey all. I’ve been playing with the Monday API and have made a lot of progress towards getting it ready to be available in our system and a couple of other tools we use, however, I’ve hit a bit of a snag when it comes to adding files. I’ve been able to do everything else from adding items, updates, columns, boards and reading what I’ve needed, but adding files is what has finally broken me. I’m basing my code off of Uploading a file to monday.com, the hard way. The author is using NodeJS so I’ve tried to convert it as best I can.
My code below:
<cfset dataQuery = 'mutation($file:File!){add_file_to_column(file:$file,item_id:123456789,column_ID:"files"){id}}'>
<cfset boundary = "xxxxxxxxxx">
<cfset upfile = "image1.png">
<cfset base64Sample = SAMPLE_BASE64_STRING>
<cfset data = "">
<!--- Construct Query --->
<cfset data &= "--" & boundary & "">
<cfset data &= 'Content-Disposition: form-data; name="query";'>
<cfset data &= "\\r\\nContent-Type:application/json">
<cfset data &= "\\r\\n\\r\\n" & dataQuery & "">
<!--- Construct File --->
<cfset data &= "--" & boundary & "\\r\\n">
<cfset data &= 'Content-Disposition: form-data; name="variablesefile]"; filename="' & upfile & '";\\r\\n'>
<cfset data &= "Content-Type:application/octet-stream;\\r\\n\\r\\n">
<cfset data &= ToString(ToBinary(base64Sample))>
<cfset data &= "\\r\\n--" & boundary & "--\\r\\n">
<cfhttp method="post" url="https://api.monday.com/v2" result="result">
<cfhttpparam type="Header" name="Content-Type" value="multipart/form-data; boundary=#boundary#">
<cfhttpparam type="Header" name="Authorization" value="API_KEY">
<cfhttpparam type="body" value="#CharsetEncode(ToBinary(ToBase64(data)), "utf-8")#">
</cfhttp>
<cfset returnStruct = DeserializeJSON(result.filecontent)>
<cfdump var="#returnStruct#">
The formatting has gotten shifted a little bit as I tried to get it working but no matter what I do, it always gives me the same error message: “No query string was present”.
I’m hoping someone here who has more knowledge about doing multi-part boundaries and the Monday API can help.
Thanks to @basdebruin for their help! I was able to get a working bit of code!
<cfset dataQuery = "mutation ($file:File!) {add_file_to_column (item_id: 123456789, column_id: files, file: $file){id}}">
<cfset boundary = "xxxxxxxxxx">
<cfset upfile = "image1.png">
<cfset base64Sample = "SAMPLE_BASE64_IMAGE">
<cfset data = "">
<cfset cflf = "#chr(13)##chr(10)#">
<!--- Construct Query --->
<cfset data &= "--" & boundary & "#cflf#">
<cfset data &= 'Content-Disposition: form-data; name="query"#cflf##cflf#'>
<cfset data &= dataQuery & '#cflf#'>
<cfset data &= "--" & boundary & "#cflf#">
<!--- Construct File --->
<cfset data &= "Content-Disposition: form-data; name=""variables;file]""; filename=""#upfile#"";#cflf#">
<cfset data &= "Content-Type:application/octet-stream;#cflf##cflf#">
<cfset data &= ToString(ToBinary(base64Sample))>
<cfset data &= "#cflf#" &"--" & boundary & "--">
<cfdump var="#data#">
<cfhttp method="post" url="https://api.monday.com/v2/file" result="result">
<cfhttpparam type="Header" name="Content-Type" value="multipart/form-data; boundary=#boundary#">
<cfhttpparam type="Header" name="Authorization" value="API_KEY_HERE">
<cfhttpparam type="body" value="#data#">
</cfhttp>
<cfset returnStruct = DeserializeJSON(result.filecontent)>
<cfdump var="#returnStruct#">