For a project I am creating an ASP.net Core Web API.
I created a POST endpoint for request from a Webhook (Java API)
My C# Class:
public class Data
{
[JsonPropertyName("state")]
public string? state { get; set; }
[JsonPropertyName("lat")]
public double? lat { get; set; }
[JsonPropertyName("lng")]
public double? lng { get; set; }
[JsonPropertyName("accuracy")]
public int? accuracy { get; set; }
[JsonPropertyName("source")]
public string? source { get; set; }
[JsonPropertyName("id")]
public int? id { get; set; }
[JsonPropertyName("time")]
public DateTime? time { get; set; }
[JsonPropertyName("insertTime")]
public DateTime? insertTime { get; set; }
[JsonPropertyName("serial")]
public string? serial { get; set; }
}
So I thought datetime could dealing with following format: 2023-03-01T15:25:00+0000 But I always get an error from this type:
"The JSON value could not be converted to System.Nullable`1[System.DateTime]. Path: $.data.time | LineNumber: 0 | BytePositionInLine: 139."
Someone that have a solution?
Thanks in Advance.
UPDATE:
JSON :
{
"dataType":"location",
"data":{
"state":"STOP",
"lat":50.00,
"lng":5.00,
"source":"gps",
"id":1804885154,
"time":"2023-03-01T11:12:00+0000",
"insertTime":"2023-03-01T11:18:30+0000",
"serial":"xxxx"
}
}
FULL C# CLASS:
public class SensolusSensorWebhook
{
[JsonProperty("dataType")]
public string? DataType { get; set; }
[JsonProperty("data")]
public Data? Data { get; set; }
}
public class Data
{
[JsonPropertyName("state")]
public string? state { get; set; }
[JsonPropertyName("lat")]
public double? lat { get; set; }
[JsonPropertyName("lng")]
public double? lng { get; set; }
[JsonPropertyName("accuracy")]
public int? accuracy { get; set; }
[JsonPropertyName("source")]
public string? source { get; set; }
[JsonPropertyName("id")]
public int? id { get; set; }
[JsonPropertyName("time")]
public DateTime? time { get; set; }
[JsonPropertyName("insertTime")]
public DateTime? insertTime { get; set; }
[JsonPropertyName("serial")]
public string? serial { get; set; }
[JsonPropertyName("thirdPartyId")]
public string? thirdPartyId { get; set; }
}
Swagger UI Info: Foto van endpoint in swagger
Endpoint code:
[HttpPost("sensordata/sensolus")]
public async Task<ActionResult> PostSensolusWebhook([FromBody] SensolusSensorWebhook? sensorWebhook)
{
_logger.LogInformation("Falls in PostSensolusWebhook");
_logger.LogInformation($"Sensorwebhook: {sensorWebhook.ToString()}");
await _sensorTestDataService.CreateAsync(sensorWebhook);
return Ok(sensorWebhook);
}