404 Error when trying to post a file topic

2019-03-04 16:39发布

问题:

I am trying to create content in my d2l orgUnit but I keep getting 404 Page not found back. I am calling this from the Android emulator. I've successfully created a module as well as a topic link using the POST APIs. I did look at this post 404 Posting Content to Desire2Learn which helped me get the module and link working, but I just can't get the uploading a file as a topic to work. I suspect it may be the URL as I wasn't sure what to put, so I put a relative path I created in the org unit. The post 404 Posting Content to Desire2Learn mentions to use "valid location URL to within the org unit's existing content space". I also tried the /content/enforced/... folder as URL to no avail. I'm not sure if this is the issue, or a red herring...

Here is my code:

String body = "--xxBoundaryxx " +
    "Content-Type: application/json " +
    "{" +
    "\"Title\": \"Testing an upload\"," +
    "\"ShortTitle\": \"test\"," +
    "\"Type\": 1," +
    "\"TopicType\": 1," +
    "\"URL\": \"/test/\"," +
    "\"StartDate\": null," +
    "\"EndDate\": null," +
    "\"IsHidden\": false," +
    "\"IsLocked\": false" +
    " } " +
    "--xxBoundaryxx " +
    "Content-Disposition: form-data; name=\"file 0\"; filename=\"test.txt\" " +
    "Content-Type: text/plain " +
    "This is my sample file to try it out.";

URI uri = userContext.createAuthenticatedUri("/d2l/api/le/1.1/{OrgID}/content/modules/{ModuleID}/structure/", "Post");


DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(uri);
post.addHeader("Content-Type", "multipart/mixed; boundary=xxBoundaryxx");
post.setEntity(new StringEntity(body));

HttpResponse response = httpClient.execute(post);
Log.i(TAG, "Statusline: " + response.getStatusLine());

Here is the resulting body (I put \r\n for line breaks as well, but it didn't help).

--xxBoundaryxx 
Content-Type: application/json 
{
    "Title": "Testing an upload",
    "ShortTitle": "test",
    "Type": 1,
    "TopicType": 1,
    "URL": "/test/",
    "StartDate": null,
    "EndDate": null,
    "IsHidden": false,
    "IsLocked": false
} 
--xxBoundaryxx 
Content-Disposition: form-data; name="file 0"; filename="test.txt" 
Content-Type: text/plain 
This is my sample file to try it out.

what is going on? getStatusLine always returns 404 error... I know it is not a permission issue since I can create modules and link topics successfully using very similar code. Any guidance greatly appreciated.

回答1:

You almost certainly need a terminating boundary on your POST body (--xxBoundaryxx--). For the Url property in the JSON block you send: you can send an Url that's not relative (as in the example in the docs); it also seems you can send just a file name (relative name) and the upload process puts the file in the root of the course's content area. I would fully expect that (a) if you don't have an absolute URL, it would use the course's root folder as the base part of the path, and (b) the upload API action will not create directories for you, but I haven't comprehensive testing around how the Url property gets handled.

I've tested this API action on our test servers, and it works (with a fully specified Url and just a file name for the Url). I've also updated the Valence docs to include a concrete example of the course content file topic upload packet: hope these two things help you out.