File Upload API

Jump to solution
garth
Community Champion


I have been working through the API portion of this course: Canvas Dev and Friends

(I completed the LTI portion : )

Lesson 6 walks you through file uploads, and I am stuck.

My initial request to upload a file is returning the upload url and upload_params, no problem.

However when I try to post the file I get back StatusCode: 403, ReasonPhrase: 'Forbidden'

I am following the documentation found here: Uploading Files - Canvas LMS REST API Documentation

and am not sure why this isn't working.

I am working with .NET, using the HttpClient, and have verified that the upload_params are being added in the order they were received, and that "file" is the last parameter to be added to the HttpContent.  I have added the file as "StreamContent", and "ByteArrayContent".

Has anyone else experienced this?

1 Solution
garth
Community Champion
Author

Well, once you know the root cause the solution comes easily.

Add a WebRequestHandler to the constructor of HttpClient, and don't allow redirects:

WebRequestHandler handler = new WebRequestHandler();

handler.AllowAutoRedirect = false;

using (HttpClient client = new HttpClient(handler))

....

Now I know, I hope someone else finds this useful.

View solution in original post