cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Community Member

Error 500 when uploading a file via API

Jump to solution

I swear to the LMS gods that the following Python requests code was working just yesterday:

import requests

# Step 1: Tell Canvas you're going to upload a file

filename = 'test.svg'
folderPath = '/myFolder/test'

size = os.path.getsize(filename)
data = { 'name' : filename,
'size' : str(size),
'content_type' : 'image/svg+xml',
'parent_folder_path' : folderPath }

response = requests.post(filesURL, json=data, headers=headers)
response.raise_for_status()
response = response.json()


# Step 2: Upload the file

files = list(response['upload_params'].items()) # Get all upload params
file_content = open('svg/'+filename, 'rb').read() # Read in file data
files.append((u'file', file_content)) # Add file data to payload
response = requests.post(response['upload_url'], files=files)
response.raise_for_status()
id = str(response.json()['id'])

However, in step 1, when I get the first response, it looks nothing like it used to or what's in the documentation:

{
'file_param': 'file',
'progress': None,
'upload_url': 'https://inst-fs-iad-prod.inscloudgate.net/files?token=<crazy long token>',
'upload_params':
{
'filename': 'c02s04n02a.svg',
'content_type': 'image/svg+xml'
}
}

The code produces the following error:

500 Server Error: Internal Server Error for url: https://inst-fs-iad-prod.inscloudgate.net/files?token=<blah etc.>

Am I missing something? Has a change of file repository occurred from AWS?

Would appreciate some help!

Thanks

Labels (1)
Tags (2)
13 Replies
Highlighted

That definitely helps clarify the issue, thanks. Right now, I can't access Canvas at all -- getting server back end issues or the site cannot be reached. At least that is documented on their status page. Even if I could access it, I probably won't be able to help with this one. The only issues I've personally encountered with inscloudgate was getting messages about blocked third-party cookies. I was just trying to tell if it was related to the other file upload issues going on and it doesn't sound like it. Hopefully someone else has ideas.

Highlighted

I had the same errors, and found that a "filename" parameter which Requests adds to multipart/form-data parts was the problem.

To prevent Requests from adding this, the files argument for requests.post can be provided with tuples of values, giving None in the first position, so we have a list like [('Name', (None, 'value')), ('Name2', (None, 'value2'))].

So in the code sample in the original post, it might help changing line 21 to

files = [(key, (None, val)) for key, val in response['upload_params'].items()] # Get all upload params

I figured this out from this Stackoverflow answer, so thanks to Martijn Pieters!

View solution in original post

Highlighted

I figured this out finally. It appears that posting to inscloudgate requires that you include the "filename" as part of the form data of the actual file (even though this is already specified as separate form data per Canvas uploading files documentation).

So instead of:

--cb243f9c-3bd8-48be-a88e-19c7579e0d68
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=filename
12345.jpg
--cb243f9c-3bd8-48be-a88e-19c7579e0d68
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=content_type
image/jpeg
--cb243f9c-3bd8-48be-a88e-19c7579e0d68
Content-Disposition: form-data; name=file
<image memory stream>
--cb243f9c-3bd8-48be-a88e-19c7579e0d68--
It should be:
--cb243f9c-3bd8-48be-a88e-19c7579e0d68
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=filename
12345.jpg
--cb243f9c-3bd8-48be-a88e-19c7579e0d68
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=content_type
image/jpeg
--cb243f9c-3bd8-48be-a88e-19c7579e0d68
Content-Disposition: form-data; name=file; filename=12345.jpg; filename*=utf-8''12345.jpg
<image memory stream>
--cb243f9c-3bd8-48be-a88e-19c7579e0d68--
Of course I'm not manually creating the above streamed data but rather using .NET's HttpClient and you just need to specify the filename for the image memory stream, the filename*=utf-8''12345.jpg is done automatically by the HttpClient.
Highlighted

Ah, thank you! This has solved the issue for me! :smileygrin:

What a strange format! But, this definitely seems like the best solution as it is somewhat resilient to future changes in the API.

You have saved us!