0

我编写了一个 API 来在 Desire2Learn 中创建 CreateCourseOffering

http://docs.valence.desire2learn.com/res/course.html#post--d2l-api-lp-(D2LVERSION-version)-courses-

我为此调用了上面的 API。代码执行得很好,我没有找到任何创建的课程。在下面粘贴我的代码。请建议其中有什么问题

我的输入

        objCourse.Code = "1234567";
        objCourse.Name = "TCS_TestCourse2";
        objCourse.path = "/content/Traning";
        objCourse.ForceLocale = false;
        objCourse.ShowAddressBook = false;
        objCourse.CourseTemplateId = 10;

我的代码

    UriBuilder baseUri = new UriBuilder(userContext.CreateAuthenticatedUri("/d2l      /api/lp  /1.0/courses/", "POST"));

            CreateCourse objCreateCourse = new CreateCourse();
            objCreateCourse.Code = objCourse.Code;
            objCreateCourse.Name = objCourse.Name;
            objCreateCourse.path = objCourse.path ;
            objCreateCourse.ForceLocale = objCourse.ForceLocale;
            objCreateCourse.ShowAddressBook = objCourse.ShowAddressBook;
            objCreateCourse.CourseTemplateId = objCourse.CourseTemplateId;

            JavaScriptSerializer serializer1 = new JavaScriptSerializer();
            string createCourseData = serializer1.Serialize(objCreateCourse);




            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseUri.Uri);

            request.Method = "POST";
            request.AllowAutoRedirect = false;
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(createCourseData);
            request.ContentLength = bytes.Length;
            request.ContentType = "application/json";

            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(bytes, 0, bytes.Length);
            // Close the Stream object.
            dataStream.Close();
4

1 回答 1

1

The JSON parsing on the server side requires all fields to be included even if they are null. It looks like you are just missing a few of those fields.

I verified locally that the following JSON block successfully created a Course Offering. When I omitted the null fields it resulted in a 400 Bad Request:

var data = { 
        'Name': "TCS_TestCourse2",
        'Code': "1234567",
        'Path': "/content/Training",
        'CourseTemplateId': 10,
        'SemesterId': null,
        'StartDate': null,
        'EndDate': null,
        'LocaleId': null,
        'ForceLocale': false,
        'ShowAddressBook': false
    };

Assuming that CourseTemplateId: 10 is a valid orgunit number.

I would change this section of code to reflect that:

        CreateCourse objCreateCourse = new CreateCourse();
        objCreateCourse.Code = objCourse.Code;
        objCreateCourse.Name = objCourse.Name;
        objCreateCourse.path = objCourse.path ;
        objCreateCourse.ForceLocale = objCourse.ForceLocale;
        objCreateCourse.ShowAddressBook = objCourse.ShowAddressBook;
        objCreateCourse.CourseTemplateId = objCourse.CourseTemplateId;
        objCreateCourse.StartDate = null;
        objCreateCourse.EndDate = null;
        objCreateCourse.LocaleId = null;
        objCreateCourse.SemesterId = null;
于 2012-05-08T23:58:42.840 回答