As I mentioned, I'm working with .NET
Here is the code to upload the file to the "upload_url" that I receive from the first step in the process.
NOTE: [dres] represents the results returned by Canvas in the initial request:
//PHASE 2: upload file to upload_url and get back the location needed for PHASE 3
using (HttpClient client = new HttpClient())
{
MultipartFormDataContent content = new MultipartFormDataContent();
//add upload_params to form data in same order we received it
foreach (dynamic key in dres.upload_params)
{
content.Add(new StringContent(key.Value.ToString()), key.Name.ToString());
}
//read file to upload into byte array
FileStream fs = fi.OpenRead();
byte[] fileBytes = new byte[fi.Length];
int numBytes = fs.Read(fileBytes, 0, fileBytes.Length);
fs.Close();
//add byte array as last element of the form data, per Canvas document
content.Add(new ByteArrayContent(fileBytes), "file");
//post for data to the upload_url defined by Canvas
HttpResponseMessage response = await client.PostAsync(dres.upload_url.ToString(), content);
string s = await response.Content.ReadAsStringAsync();
}
The response I receive, actually from AWS is as follows.
Reponse Headers:
{StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization, Accept-Encoding
Access-Control-Allow-Methods: POST, PUT, DELETE, GET, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Request-Method: *
Status: 200 OK
Vary: Accept-Encoding
X-Canvas-Meta: q=945;a=1;g=2xCP3OOBwsD1e64Bp1ewJCIZXn3VfkvonsHLrRNG;s=6124;c=cluster24;z=us-east-1b;o=files;n=api_create_success;b=1059700;m=1059700;u=0.12;y=0.01;d=0.13;
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Request-Context-Id: 1ee795eb-a183-47a9-88d9-c3586816891b
X-Request-Processor: a023c27
X-Robots-Tag: none
X-Runtime: 0.387712
X-UA-Compatible: IE=Edge,chrome=1
X-XSS-Protection: 1; mode=block
Connection: keep-alive
Cache-Control: must-revalidate, max-age=0, private
Date: Tue, 14 Jun 2016 17:01:50 GMT
ETag: "1c749fd27a7b93ccd35441126e00a559"
P3P: CP="None, see http://www.instructure.com/privacy-policy"
Set-Cookie: _csrf_token=Q8s%2BNe8I63l7jOewJsr3IjFBYd6U3dqKnoczaflynkQrilh6nWm7SBT8sP9nsJFkBzsghN6F9b2rz0cDoRnmKQ%3D%3D; path=/; secure
Server: Apache
Content-Length: 553
Content-Type: application/json; charset=utf-8
}}
And the response body as follows:
{
"id": 47073,
"folder_id": 322,
"display_name": "test2.txt",
"filename": "test2.txt",
"content-type": "text/plain",
"url": "https://xxx.test.instructure.com/files/47073/download?download_frd=1\\u0026verifier=kBJ375302R4NbJay...",
"size": 11,
"created_at": "2016-06-14T16:59:59Z",
"updated_at": "2016-06-14T17:01:50Z",
"unlock_at": null,
"locked": false,
"hidden": false,
"lock_at": null,
"hidden_for_user": false,
"thumbnail_url": null,
"modified_at": "2016-06-14T16:59:59Z",
"locked_for_user": false,
"preview_url": "/users/xx/files/xxxxx/file_preview?annotate=0"
}
The problem is that the response status is 200, not 301
Also, the response body does not contain the "Location" value.
To quote the Canvas document: Uploading Files - Canvas LMS REST API Documentation
"The parameters POSTed with this request come directly from the upload_params
part of the JSON response in Step 1. The only addition is the file
parameter which must be posted as the last parameter following all the others."
I have followed these instructions, and using the code above, the file does upload and appears and is accessible in the target Canvas folder.
But why am I not getting a status 301 response with the "Location"??
If anyone has any ideas I would appreciate any feedback : )