简体   繁体   中英

C# HttpWebRequest to Scorm Cloud always returning error 400 bad request

I am trying to post Tin Can Statements to a Scorm Cloud LRS via c# HttpWebRequest in JSON format. However I always get error 400. The authentication is correct so what is wrong with the JSON? I have tried encoding as UTF8 also and still no dice

Here is the code and credentials needed:

    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://cloud.scorm.com/ScormEngineInterface/TCAPI/RCFZ5D8GXU/sandbox/");
    httpWebRequest.ContentType = "application/json; charset=UTF-8";
    httpWebRequest.Method = "POST";
    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    String autorization= "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("RCFZ5D8GXU222" + ":" + "YQA3VfX1NiuYkKXEEzkKu723NwejpwNkB6x0Vhg3"));

    httpWebRequest.Headers.Add("Authorization", autorization);
    httpWebRequest.Headers.Add("X-Experience-API-Version", "1.0.1");
    string jsonText = "{"+
                        "   \"actor\": {"+
                            "   \"mbox\": \"mailto:Steeno@gmail.com\","+
                            "   \"name\": \"Austin Glatt\","+
                            "   \"objectType\": \"Agent\""+
                            "},"+
                            "\"verb\": {"+
                            "   \"id\": \"http://adlnet.gov/expapi/verbs/attempted\","+
                            "   \"display\": {"+
                            "       \"en-US\": \"attempted\""+
                            "   }"+
                            "},"+
                            "\"object\": {"+
                            "   \"id\": \"http://www.example.com/tincan/activities/cMjKwAGI\","+
                            "   \"objectType\": \"Activity\","+
                            "   \"definition\": {"+
                            "       \"name\": {"+
                            "           \"en-US\": \"Part Removal\""+
                            "       },"+
                            "       \"description\": {"+
                            "           \"en-US\": \"On Engine 155\""+
                            "       }"+
                            "   }"+
                            "}"+
                        "}";
    byte [] jsonData = System.Text.Encoding.UTF8.GetBytes(jsonText);
    Debug.Log(System.Text.Encoding.UTF8.GetString(jsonData));
    httpWebRequest.ContentLength = jsonData.Length;
    using (var streamWriter = httpWebRequest.GetRequestStream())
    {
        streamWriter.Write(jsonData,0,jsonData.Length);
        streamWriter.Flush();
        streamWriter.Close();
    }


     try
        {
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                Debug.Log("POST result"+result);
            }
    }
    catch(WebException ex)
    {

        if (ex.Response != null)
        {
            Debug.Log(ex.Message);
            foreach(DictionaryEntry d in ex.Data)
                Debug.Log(d.ToString());

                 string errorDetail = string.Empty;
                using (StreamReader streamReader = new StreamReader(ex.Response.GetResponseStream(), true))
                {
                    errorDetail = streamReader.ReadToEnd();
                    Debug.Log(errorDetail);
                }
        }
    }

I think you are missing the "statements" on the URL, your request should be going to:

https://cloud.scorm.com/ScormEngineInterface/TCAPI/RCFZ5D8GXU/sandbox/statements

Original answer in case anyone cares:

It appears you are posting a 0.95 or 1.0.x statement, but you don't seem to be setting an X-Experience-API-Version header, so the LRS is probably interpreting it as a 0.9 statement which would be invalid.

I'd highly recommend using a library to build the requests along with the content of the request. We have one available here:

http://rusticisoftware.github.io/TinCan.NET/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM