0

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.

Community
  • 1
  • 1
Paul B
  • 35
  • 3

1 Answers1

0

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.

Viktor Haag
  • 3,363
  • 1
  • 18
  • 21
  • Thanks for the feedback. I tried adding the --xxBoundaryxx-- to terminate the POST body and put Url element to simply "test.txt" and I still get the same 404. I looked at the concrete example (many, many times!) , but I just can't seem to get it to work. Since my original post I also tried the 3rd example on that page (uploading news item) to take the Url out of the picture and I still got the 404. I only removed the image portion and changed to my OrgId. Would it be possible to post a snippet of code (java preferred) that would accomplish just preparing the HttpPost instance (or equivalent)? – Paul B May 15 '13 at 16:55
  • I also tried the news post [on the samples site](http://samples.valence.desire2learn.com/samples/GettingStartedSample/) but it returned a 400 Unknown error. I suspect it is because there is no way of adding the Content-Type: multipart/mixed; boundary=xxBoundaryxx header parameter. – Paul B May 15 '13 at 17:41
  • Thanks for the example. I tried it verbatim for our server except I changed the /content/... folder to reflect the root folder structure I see when I am in the Manage Files section. I always get the 404 error and have no idea what is wrong. Is there any way I can try this on our back-end using [something like this](http://samples.valence.desire2learn.com/samples/GettingStartedSample)? Every multipart post fails and unfortunately I can't test on the [Getting Started Page](http://samples.valence.desire2learn.com/samples/GettingStartedSample) – Paul B May 15 '13 at 21:03
  • Paul -- we're in the middle of a transition to a new support resource for more in-depth discussion and assistance with the Valence platform than can really be accommodated here. In the meantime, I suggest you please email `valence@desire2learn.com` with an explanation of your difficulties, including links to the relevant questions you've left here. We should then be able to help you out without a lot of back-and-forth that doesn't make sense for Stack. – Viktor Haag May 16 '13 at 12:41
  • This was resolved [here](http://stackoverflow.com/questions/16577298/error-302-when-trying-to-post-d2l-topic-using-multipart). As Viktor pointed out there, the code was missing some required blank lines. Thanks again for your help! – Paul B May 16 '13 at 13:08
  • I've since decided to use the MultiPartEntity instead of using the above code. It is much cleaner and supports larger file uploads. [See here for more info.](http://stackoverflow.com/questions/5416038/uploading-image-to-server-in-multipart-along-with-json-data-in-android/17686958#17686958). – Paul B Jul 16 '13 at 21:18