API problem: attachments in create conversations

Jump to solution
josh3
Community Novice

I'm trying to use the Canvas REST API to automate the sending of emails on student progress,  that is sending "Conversations" to users. I can do so successfully without attachments, but when I try to include an attachment, which has been uploaded to my “conversation attachments” folder, it gives me a response 500 error. 

To try to make sure that I'm doing this correctly, I've done the following.

1. I first manually send a test email and attach an attachment which is a png file of size roughly 20KB. The attachment gets uploaded to my "conversation attachments" folder. I can then query it using the REST API and find its id and other attributes. The test email is sent correctly.

2. I upload a similar attachment file to my "conversations attachments" folder using the REST API and checked that it appears in the folder and has identical attributes (e.g. mime_class, hidden, etc.) So both the attachment in 1, and the REST API uploaded attachment, are in the same "conversations attachments" folder and do not seem to have any obvious differences between them. 

3. If I try to send an message using the API "Create a conversation", it works fine with no attachment, but with using either the attachment id of the attachment created in 1, or using  2 as explained above, the email does not get sent and I get a response 500 error and the email is not sent. The body of the POST that I send has as its body:

body=This+is+a+test+of+the+email+system.+Please+ignore.&recipients=3204641&subject=This+is+a+test.+Ignore.&group_conversation=False&attachment_ids=58928187&mode=sync

 

Any help trying to understand what I'm doing wrong would be greatly appreciated!

Josh

Labels (1)
1 Solution
josh3
Community Novice
Author

I did contact Canvas support and they gave me the solution. It involves changing the raw http POST. If you modify the chrome javascript console query above:

$.post("/api/v1/conversations?body=testoftheemailsystem.Pleaseignore.&recipients=1586543&subject=Thisisatest.Ignore.&group_conversation=False&attachment_ids=58928187&mode=sync",

function(data) { console.log(data); },'json');

to

$.post("/api/v1/conversations?body=testoftheemailsystem.Pleaseignore.&recipients=1586543&subject=Thisisatest.Ignore.&group_conversation=False&attachment_ids[]=58928187&mode=sync",

function(data) { console.log(data); },'json');

Note the extra "[]" that is added after attachment_ids. This is another way of sending posts of array-like data. This fixes the bug and it works.

The standard python library that was getting invoked by the Canvas Python SDK ,  was "requests". It made this query string without the "[]" as presumably that normally works fine with the Canvas REST API. However for this particular function, it doesn't. Apparently from above, the same problem occurs with using curl. I was able to kludge this in the Canvas Python SDK by passing in optional kwargs. So all is good! Hopefully this problem will be addressed at some point by Canvas so this is no longer a problem.

Thanks for everyone's help!

Josh

View solution in original post