This seems like it should be easy. I need to POST data like this to a webapi controller:
{ "startRace":"2016-08-22T12:00:00.000Z", "endRace":"2016-08-26T12:00:00.000Z" }
So I created a console app and here is the snippet of code that handles the POST event:
var i = (int)DateTime.Now.DayOfWeek;
var startRace = DateTime.Today.AddDays(i);
var endRace = DateTime.Today.AddDays(i + 4);
var raceDates = new Dictionary<string, string>
{
{"startRace", startRace.ToString("u")},
{"endRace", endRace.ToString("u")}
};
var json = JsonConvert.SerializeObject(raceDates);
using (var http = new HttpClient())
{
try {
HttpResponseMessage response = http.PostAsync("http://localhost:15312/api/race/dates/post",
new StringContent(json, Encoding.UTF8, "application/json")).Result;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
But everytime I run the app, I always get this error message:
String is not in JSON format
Is there something I'm missing?
Thanks!
new StringContent(raceDates, Encoding.UTF8, "application/json")