user6742834 user6742834 - 1 month ago 12x
reST (reStructuredText) Question

Bad Request -Post method - JSON DateTime issue

I have a Visual Studio (2015) project that includes a client part (Xamarin.Forms PCL) and a web service part ( WCF Rest). The web services use edmx to communicate with the database ( SQL Server 2016). JSON is used to exchange the data.

I'm new to creating/consuming WCF Rest services. I have no issue using the GET method but I'm stuck with an issue with a POST method.

This method is part of a service that works well: no issue for a GET based method. It works well when I test it from a URL or from my client ( PCL Xamarin.Forms).

The POST method (my first ever) is a bit more problematic.

It's supposed to create a new record in a table in SQL Server (2016).

When I use Postman ( to test it, it already has an issue: it creates a record in the table but the object has two dates and the two dates are replaced by 1970-01-01.

When I use my client to contact the web service:I get 'Bad Request'.

I looked for a solution and found that instead of placing the Datetime value, it was best to place the number of milliseconds from 1970-01-01.

I used this advice in Postman and noticed the creation of a new line worked fine.

Body of the Postman request :

"Reservation_Utilisateur_Id" : "4",
"Reservation_Velo_Id" : "2",
"Reservation_DateDebut" : "\/Date(1245398693390)\/",
"Reservation_PeriodeDebut" : "matin",
"Reservation_DateFin" :"\/Date(1245398693390)\/",
"Reservation_PeriodeFin" : "matin"

Now, I'd like to know how to get that object to send to the server . How can my object be serialized like above?

I looked for a solution unsuccessfully.

I keep on getting "There was an error deserializing the object of type BikeSharingService.Reservation. DateTime content '2016-08-22T00:00:00+02:00' does not start with '/Date(' and end with ')/' as required for JSON."

Could someone please give the newbie that I am an explanation and maybe some code that works?

Here is my code:

My contract:

[WebInvoke(Method = "POST", UriTemplate = "create",
ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
Reservation create(Reservation reservation);

The service method:

public Reservation create(Reservation reservation)
using (BikeSharingEntities bse = new BikeSharingEntities())
Reservation re = new Reservation
Reservation_Utilisateur_Id = reservation.Reservation_Utilisateur_Id,
Reservation_Velo_Id = reservation.Reservation_Velo_Id,
Reservation_DateDebut = reservation.Reservation_DateDebut,
Reservation_PeriodeDebut = reservation.Reservation_PeriodeDebut,
Reservation_DateFin = reservation.Reservation_DateFin,
Reservation_PeriodeFin = reservation.Reservation_PeriodeFin,
Reservation_DemandeRecupDomCli = reservation.Reservation_DemandeRecupDomCli

return re;

On the client side :

const string Url1 = "http://localhost:51843/ServiceReservation.svc/create";

public async Task<Reservation> create(Reservation reservation)
string json = JsonConvert.SerializeObject(reservation);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");
var response = await client.PostAsync(Url1,
new StringContent(
Encoding.UTF8, "application/json"));
return JsonConvert.DeserializeObject<Reservation>(
await response.Content.ReadAsStringAsync());

Then calling the method on the client side :

Reservation re =new Reservation();
re.Reservation_Utilisateur_Id = 4;
re.Reservation_Velo_Id = 2;
re.Reservation_DateDebut = DateTime.Now.Date;
re.Reservation_PeriodeDebut = "matin";
re.Reservation_DateFin = DateTime.Now.Date;
re.Reservation_PeriodeFin = "matin";
re.Reservation_DemandeRecupDomCli = 1;

Reservation resultat = await reManager.create(re);

What I get :

False Bad Request Method: POST, RequestUri:
'http://localhost:51843/ServiceReservation.svc /create', Version: 2.0,
Content: System.Net.Http.StringContent, Headers: { Accept:
application/json Content-Type: application/json; charset=utf-8

Content-Length: 407 } BadRequest

There was an error deserializing the object of type
BikeSharingService.Reservation. DateTime content
'2016-08-22T00:00:00+02:00' does not start with '/Date(' and end with
')/' as required for JSON.


[Promoted from a comment]

Json doesn't define a standard date format, but it's worth noting that Json.Net (which is used by most of the web-facing parts of the .Net framework) supports multiple formats out of the box (and even custom ones).

If you can decide on a standard which works for all your clients, you can configure the Json (en/de)coding in .Net to use it natively.

See for more information and details on how to specify a date format handler.

Sample code below...

public void WriteJsonDates()
    LogEntry entry = new LogEntry
        LogDate = new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc),
        Details = "Application started."

    // default as of Json.NET 4.5
    string isoJson = JsonConvert.SerializeObject(entry);
    // {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}

    JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
        DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
    string microsoftJson = JsonConvert.SerializeObject(entry, microsoftDateFormatSettings);
    // {"Details":"Application started.","LogDate":"\/Date(1234656000000)\/"}

    string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());
    // {"Details":"Application started.","LogDate":new Date(1234656000000)}