2

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 (item_id: 123456789, column_ID: file, file: $file){id}}'>
<cfset boundary = "xxxxxxxxxx">
<cfset upfile = "image1.png">
<cfset base64Sample = SAMPLE_BASE64_STRING>
<cfset data = "">

<!--- Construct Query --->
<cfset data &= "--" & boundary & "#chr(13)##chr(10)#">
<cfset data &= 'Content-Disposition: form-data; name="query"#chr(13)##chr(10)##chr(13)##chr(10)#'>
<cfset data &= dataQuery & '#chr(13)##chr(10)#'>
<cfset data &= "--" & boundary & "#chr(13)##chr(10)#">

<!--- Construct File --->
<cfset data &= 'Content-Disposition: form-data; name="variables[file]"; filename="' & upfile & '";#chr(13)##chr(10)#'>
<cfset data &= "Content-Type:application/octet-stream;#chr(13)##chr(10)##chr(13)##chr(10)#">
<cfset data &= ToString(ToBinary(base64Sample))>
<cfset data &= "#chr(13)##chr(10)#--" & boundary & "--#chr(13)##chr(10)#">

<cfdump var="#data#">
<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="#mondayLogin.Token#">
    <cfhttpparam type="body" value="#data#">
</cfhttp>

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.

Edit: I changed the \r\n to the chr(13)chr(10) and am still getting the same error.

Edit 2: After a little more refactoring on it I seem to have gotten past the first error and now I've hit 2 new one that are related to GraphQL I think.

  • Field 'add_file_to_column' is missing required arguments: column_id
  • Field 'add_file_to_column' doesn't accept argument 'column_ID' I'm sure I'm still formatting it wrong somewhere but I don't see where that is yet.

Edit 3: Turns out my column name was wrong. The 'ID' shouldn't have been in caps but should be 'column_id'. I'm now getting another new error of: Internal server error (500)

jkw4703
  • 352
  • 3
  • 17
  • 2
    I don't know anything about `monday.com` but one thing about your code did jump out at me. In the sample page wherever they use `\r\n`, they are escape characters for the carriage return and linefeed characters in JavaScript. In ColdFusion, you would instead concatenate with `chr(13) & chr(10)`. At the very minimum you need to replace those it it will at least help you get closer to your solution. – user12031119 Mar 28 '22 at 02:55
  • @user12031119 I replaced the \r\n and I'm still not having any luck. – jkw4703 Mar 28 '22 at 13:45

1 Answers1

4

So I ran into MANY issues on this with a lot of different error messages. Basically I had 3 issues:

  1. Replace my \r\n with the correct ColdFusion values of chr(13)chr(10)
  2. The formatting of the query was incorrect according to the documentation
  3. ColdFusion treats single quotes(') and double quotes(") differently when compiling them. Once I escaped the double quotes and removed the single quotes I was able to get it working. Below is my working demo.
<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#">
jkw4703
  • 352
  • 3
  • 17
  • 1
    Glad you resolved it. Though not sure I agree with #3 :) Either quote type should be fine as long things are nested properly and [these test strings are identical](https://trycf.com/gist/9d5c2c5c154b082d76eb606be404268a/acf2018?theme=monokai) – SOS Mar 28 '22 at 17:55