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.

有帮助吗?

解决方案

The 302 redirect is really a redirect to a 404 error page. I suspect that, because you're getting the 302 redirect to a system "404 error" page, the redirection is happening at the route-handling layer: it's possible to get a 404 from deeper down in the stack, but then you'd just get a direct 404 response to your request. The most likely causes, then, are that the D2L app stack can't properly bind your URL (plus a few parameters) to a route handler -- that is, it can't tokenize your request to know which bit of controller code to hand the request off to.

I presume that, when you write

userContext.createAuthenticatedUri("/d2l/api/le/1.0/Orgid/content/modules/moduleid/structure/", "POST")

you are actually putting in the real Orgid and moduleid values in that route? Because the string as written would definitely return a 404 -- there's no such route -- and in fact it would likely redirect to the system 404 error page exactly as you describe.

Notice also that when you write

 ...
 "Content-Type: application/json \r\n" +
 "{\"IsHidden\": false, \"IsLocked\": false, \"ShortTitle\": \"Test\", \"Type\": 1, \r\n" +
 ...

I'm not sure that will work. I believe that the relevant standards on multipart HTTP bodies indicate that you're required to mark the separation of a part header from a part data payload with a blank line, and here you're only inerting one carriage return. Your packet will look like this:

Content-Type: application/json
{"IsHidden": false, "IsLocked": false, ...

When it should maybe look like this:

Content-Type: application/json

{"IsHidden": false, "IsLocked": false, ...

You might try fixing those and see what happens.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top