1

I am attempting to implement my own LRS for saving TinCanAPI statements and in order to do this I need to retrieve the values sent in the Request Payload which stores the details of the learning activity statement. When viewing my WebAPI call in developer tools I can see the required values but I have been unable to find them using the Request object.

How Can I retrieve the Request Payload variables from the Request object? I have tried the request object and looked in the Content and Properties fields but I cannot seem to see a Request Payload property to reference in C#. My payload looks as follows:

{
    "id": "d3d9aa2a-5f20-4303-84c3-1f6f5b4e9236",
    "timestamp": "2014-06-26T11:00:41.432Z",
    "actor": {
        "objectType": "Agent",
        "mbox": "mailto:name@company.com",
        "name": "My Name"
    },
    "verb": {
        "id": "http://adlnet.gov/expapi/verbs/attempted",
        "display": {
            "und": "attempted"
        }
    },
    "context": {
        "extensions": {
            "http://tincanapi.com/JsTetris_TCAPI/gameId": "5686f104-3301-459d-9487-f84af3b3915c"
        },
        "contextActivities": {
            "grouping": [
                {
                    "id": "http://tincanapi.com/JsTetris_TCAPI",
                    "objectType": "Activity"
                }
            ]
        }
    },
    "object": {
        "id": "http://tincanapi.com/JsTetris_TCAPI",
        "objectType": "Activity",
        "definition": {
            "type": "http://adlnet.gov/expapi/activities/media",
            "name": {
                "en-US": "Js Tetris - Tin Can Prototype"
            },
            "description": {
                "en-US": "A game of tetris."
            }
        }
    }
}

I have tried using:

var test1 = Request.ToString(); **EMPTY STRING**

var test1 = Request.Content.ReadAsStringAsync().Result; **EMPTY STRING**

var test2 = Request.Content.ReadAsFormDataAsync().Result; **THROWS FORMATTER ERROR DESPITE config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json")); added in the webapiconfig.cs
Jay
  • 3,012
  • 14
  • 48
  • 99
  • Refer to this: http://stackoverflow.com/questions/17971548/how-to-get-httprequestmessage-data# And after you get the content as a string you can use System.Web.Script.Serialization class's serialization and deserialization methods. – Shubham Mar 20 '17 at 09:01

3 Answers3

0

I was able to retrieve the sent statement values by changing my WebAPI controller as follows:

public void Put([FromBody]TinCan.Statement statement)
{
   var actor = statement.actor;
   var context = statement.context;
   var target = statement.target;
   var timestamp = statement.timestamp;
   var verb = statement.verb;

   ...................
Jay
  • 3,012
  • 14
  • 48
  • 99
0
 try {
                //String payloadRequest = getBody(request);
                // System.out.println("payloadRequest : "+payloadRequest);
                StringBuilder buffer = new StringBuilder();
                 BufferedReader reader = request.getReader();
                 String line;
                 while ((line = reader.readLine()) != null) {
                     buffer.append(line);
                 }
                 String data = buffer.toString();
                 System.out.println("payloadRequest : "+data);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
Arun
  • 1,011
  • 11
  • 13
-1

Try this:

var value = Request["var_name"];
MatteoSp
  • 2,940
  • 5
  • 28
  • 36
  • Cannot apply indexing with [] to an expression of type 'System.Net.Http.HttpRequestMessage' – Jay Jun 26 '14 at 10:34
  • Note that its on the asp.net controller where I am trying to receive these values – Jay Jun 26 '14 at 10:35