jkw4703
jkw4703

Reputation: 362

Problem adding files with ColdFusion via Monday.com API

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.

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)

Upvotes: 2

Views: 305

Answers (1)

jkw4703
jkw4703

Reputation: 362

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#">

Upvotes: 4

Related Questions