This issue is related to one I posted yesterday but I have uncovered a bit more and am now getting a different error. I removed the code from Android to try straight from Java. I also tried 2 different ways for the multipost information. The userContext has write permission as I can easily create modules.
One way I tried:
String json = "{\"IsHidden\": false, \"IsLocked\": false, \"ShortTitle\": "Test\", \"Type\": 1, \r\n" + "\"DueDate\": null, \"Url\": \"file.txt\", \r\n" + "\"StartDate\": null, \"TopicType\": 1, \"EndDate\": null, \"Title\": \"Test topic \r\n" + "content\"} \r\n";
URI uri = userContext.createAuthenticatedUri("/d2l/api/le/1.0/Orgid/content/modules/moduleid/structure/", "POST");
MultipartEntity entity = new MultipartEntity();
StringBody part1 = new StringBody(json, "application/json", null);
entity.addPart("json", part1);
File file = new File("test.txt");
FileBody part2 = new FileBody(file, "test.txt", "text/plain", null);
entity.addPart("file", part2);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(uri);
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
System.out.println("Statusline: " + response.getStatusLine());
Here is the other way I tried:
String body = "--xxBOUNDARYxx \r\n" +
"Content-Type: application/json \r\n" +
"{\"IsHidden\": false, \"IsLocked\": false, \"ShortTitle\": \"Test\", \"Type\": 1, \r\n" +
"\"DueDate\": null, \"Url\": \"file.txt\", \r\n" +
"\"StartDate\": null, \"TopicType\": 1, \"EndDate\": null, \"Title\": \"Test topic \r\n" +
"content\"} \r\n" +
"--xxBOUNDARYxx \r\n" +
"Content-Disposition: form-data; name=\"\"; filename=\"file.txt\" \r\n" +
"Content-Type: text/plain \r\n" +
" This is a sample text file \r\n" +
"with some text content. \r\n" +
"--xxBOUNDARYxx--";
URI uri = userContext.createAuthenticatedUri("/d2l/api/le/1.0/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);
System.out.println("Statusline: " + response.getStatusLine());
Both of these methods gave the same result:
Statusline: HTTP/1.1 302 Found
Response: <html><head><title>Object moved</title></head><body><h2>Object moved to <a href="/d2l/error/404">here</a>.</h2></body></html>
IMHO both techniques should create structures as described here, so I am really stuck. I also tried putting the full /content/enforced/.../file.txt
for Url with the same result.