I have following problem
I'm using RestSharp for accessing my API. But when I'm sending a DateTime it seems to be converted to UTC. I'm sending '10.06.1991 00:00' and the API gets '09.06.1991 22:00'
So, I would always need to add 2 hours when my API gets a DateTime-object?
I checked the JSON RestSharp sends to the API.
public class Test
{
public int IntProperty {get;set;}
public string StringProperty {get;set;}
public DateTime DateTimeProperty {get;set;}
}
My object is
Test t = new Test{ IntProperty=3, StringProperty="string", DateTimeProperty=new DateTime(1991,06,10) }
when I'm sending the object via RestSharp, the JSON my API receives is
{
"IntProperty":3,
"StringProperty":"string",
"DateTimeProperty":"09.06.1991 22:00:00"
}
Any idea what I could do? Thanks
It's not your API that receives wrong data, it's your client that sends "wrong" data. I got the same problem with my API. No, it's correct data but converted to UTC.
The exact problem is described here: https://github.com/restsharp/RestSharp/issues/834
So, don't add 2 hours to each DateTime you get it in your API. You would perhaps change correct data when another client sends unconverted dates.
10.06.1991 00:00
- maybe you are okay with itHere is one really good example on how to implement: http://bytefish.de/blog/restsharp_custom_json_serializer/
ISerializer
and IDeserializer
in serialize you call JSON.Net Serialize
while in deserialize you call JSON.Net Deserialize
you just need to add a handler to your RestClient like this: (I'm using the static Default-instance described in the mentioned blog)
my client looks like:
private readonly RestClient _client;
public RestApiClient(string apiAdress)
{
_client = new RestClient(apiAdress);
_client.AddHandler("application/json", () => NewtonsoftJsonSerializer.Default);
}
and in requests you can set the JsonSerializer
:
IRestRequest restRequest =
new RestRequest(request.GetRestfulUrl(), request.Method) {
RequestFormat = request.DataFormat,
JsonSerializer = NewtonsoftJsonSerializer.Default
};