I am wanting to take JSON, put it in a StringContent and pass it to my API. From there, i am wanting to deserialize the JSON back over to a class and use it from there. Currently i am getting an error for trying to deserialize which from what i can tell, the logInfo is empty when reaching the API. When i try writing out the parameter, it's empty in the event log. See the Consumer and API Code below.
public void Log(LogInfo logInfo)
{
using (var client = CreateHttpClient())
{
var jsonData = JsonConvert.SerializeObject(logInfo);
HttpContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync("Logger/Log", content).Result;
if (!response.IsSuccessStatusCode)
{
throw new InvalidOperationException($"{response} was not successful.");
}
}
}
[HttpPost]
public HttpResponseMessage Log([FromBody] string logInfo)
{
//var logData = JsonConvert.DeserializeObject<LogInfo>(logInfo);
EventLog.WriteEntry("TestApp", logInfo);
EventLog.WriteEntry("TestApp", Request.Content.ReadAsStringAsync().Result);
//Log(logData);
return Request.CreateResponse(HttpStatusCode.OK);
}
Your controller method Log should take LogInfo parameter. This LogInfo model is a class that acts as DTO/POCO. So it should only contains the properties(getters and setters) you want to collect. You can then map this DTO/POCO to your business domain model.
[HttpPost]
public HttpResponseMessage Log(LogInfo logInfo)
{
...
}