0

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!

2
  • I dont think so you need to Serialize object before passing to StringContent. Try new StringContent(raceDates, Encoding.UTF8, "application/json") Commented Aug 19, 2016 at 20:31
  • @Paresh That throws a cannot convert from Dictionary to string... Commented Aug 22, 2016 at 14:09

1 Answer 1

1

This is a working example code.

Console App

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            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/?raceDates=" + raceDates,
                      new StringContent(json, Encoding.UTF8, "application/json")).Result;

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

            }
        }
    }
}

Web API

using System.Collections.Generic;
using System.Web.Http;

namespace WebApplication1.Controllers
{

    public class RaceController : ApiController
    {

        [Route("api/race/dates/post")]
        public void Post(Dictionary<string, string> raceDates)
        {
            var dates = new Dictionary<string, string>
                {
                 {"startRace", raceDates["startRace"]},
                 {"endRace", raceDates["endRace"]}
            };

        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.