404 Posting Content to Desire2Learn

拟墨画扇 提交于 2019-12-31 07:08:47

问题


I have a basic Valence App build for Desire2Learn and now I'm trying to POST data and I either get a 400 or 404 error.

If I make a GET request to /d2l/api/le/(D2LVERSION: version)/(D2LID: orgUnitId)/content/root/, with the correct orgUnitId, I can see all of the content for a course. However, when I POST a ContentObjectData of type Module, it returns a 400. The docs have nothing listed for a 400 error for that particular request, but I'm assuming that I messed up the ContentObjectData. I've tried multiple times, but it always results in a 400. The JSON block looks like this: { "Title": "API Test", "ShortTitle": "", "Type": 0, "ModuleStartDate": null, "ModuleEndDate": null, "IsLocked": false, "IsHidden": true }

If I make a GET request to /d2l/api/le/(D2LVERSION: version)/(D2LID: orgUnitId)/content/modules/(D2LID: moduleId)/structure/, with the correct orgUnitId and moduleId, I can see the module's content. When I POST a ContentObjectData of type Topic, it returns a 404. I'm doing this in Python, which there is no sample SDK code given, so I converted the PHP one.

I've been using another JSON block with a key 'Url' and then the respective value. Here's the fully encoded multipart body I've been trying to POST:

    --redacted.132.0.68062.1336325296.611.1
    Content-Disposition: form-data; name="ContentObjectData"
    Content-Type: application/json

    {"StartDate": null, "IsLocked": false, "TopicType": 3, "ShortTitle":
"", "Title": "API Test", "Url": "http://redacted.edu",
"EndDate": null, "IsHidden": true, "Type": 1}
    --redacted.132.0.68062.1336325296.611.1
    Content-Type: application/json

    {"Url": "http://redacted.edu"}
    --redacted.132.0.68062.1336325296.611.1--

Why would the same URI for GET and POST result in a 404 just for POST? It doesn't look like the structure is any different between the two calls in the docs. I tried this call as a normal POST request and as the multipart, but both result in a 404. I've tried both of these calls using 3 different users, one which has full admin privileges.


回答1:


This seems like two different questions, one about the route /d2l/api/le/{ver}/{orgUnitId}/content/root/ and one about the route /d2l/api/le/{ver}/{orgUnitId}/content/modules/{moduleId}/structure/. I'll try to address these separately:

Adding a root module. The first route gets used to add a route module to an org unit's content repository. The route has particular restrictions around the "Title" and "ShortTitle" properties: both must be non-null, and non-empty. In addition, the ShortTitle property must be in a form that can't be "trimmed" down to the empty string (for example, it can't be a string that has nothing but white-space in it). The slightly different restrictions on these strings has been recognized as an inconsistency, and a more regular approach may be forthcoming in a future release. You're safe if you take the approach that you must provide a title and short title for new root modules.

Adding a topic to an existing content structure. The second route gets used to add to an existing module's structure in an org unit's content repository. The way you use the route varies depending on whether you want to add a URL or whether you want to add a file.

To add a topic that's nothing more than URL, you need provide only the first part of the multipart/mixed POST (and in fact, you can just send a single POST body) with the application/json type:

{
   "Title": "Test link topic title",
   "ShortTitle": "Link",
   "Type": 1,
   "TopicType": 3,
   "URL": "http://fqd.url.to.resource.com/",
   "StartDate": null,
   "EndDate": null,
   "IsHidden": false,
   "IsLocked": false
}

Note that: (a) your topic must have a short title--in fact, the topic data block has the same restrictions around Title and ShortTitle properties as in the first question above; and, (b) you don't need to provide a second part containing the URL only.

To add a topic that's a file, you provide a similar JSON block as the first part of your multipart/mixed POST body, but (a) the TopicType should have the value '1' to indicate the topic is a file, (b) the URL property should point to a valid location URL to within the org unit's existing content space (this is how the back-end service knows where to store the file), and (c) the second part of the POST should contain the file data. Your POST body will end up looking something like this:

POST https://yourlms.edu/d2l/api/le/{ver}/{orgUnitId}/content/modules/{moduleId}/structure/?{auth} HTTP/1.1
Content-type: multipart/mixed; boundary=FOO
Content-length: {length}

--FOO
Content-type: application/json
{
   "Title": "Test file topic title",
   "ShortTitle": "File",
   "Type": 1,
   "TopicType": 1,
   "URL": "http://fqd.url.to.resource.com/",
   "StartDate": null,
   "EndDate": null,
   "IsHidden": false,
   "IsLocked": false
}

--FOO
Content-Disposition: form-data; name=""; filename={filename}
Content-Type: {file's content type}

{binary data}

Currently, the Valence reference docs do not clearly distinguish between these two kinds of requests, and they will soon be updated to do so.



来源:https://stackoverflow.com/questions/10484724/404-posting-content-to-desire2learn

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!