Retreive JSON Data from HttpRequestMessage
-
I'm doing a post to a WebAPI that passes a JSON object. Here's how the Post is set up:
var jsonContent = new JavaScriptSerializer().Serialize(myObject);
var request = (HttpWebRequest)WebRequest.Create(apiUrl);
request.Date = dateOffset.DateTime;
request.Method = method;
using (var streamWriter = new StreamWriter request.GetRequestStream()))
{
streamWriter.Write(jsonContent)
streamWriter.Flush();
streamWriter.Close();
}
}In my WebAPI I have an attribute on my controller that inherits from ActionFilterAttribute. This serves to do some authentication and security checks before the controller is called. Ideally I'd like to retrieve the JSON string from the Request, but I can't figure out how. I tried this, but it didn't work:
var contentTask = actionContext.Request.Content.ReadAsStringAsync();
contentTask.Wait();
var jsonContent = contentTask.Result;However, the content is always empty. What am I doing wrong? EDIT: For what it is worth, if I comment out this problematic code and let the call go on to my controller, the object is passed in and deserialized just fine.