favorite
I am getting 200 ok from the "postresponse" object. Can anyone please guide me how to i catch the data and save it to my database?
I am getting a response from the postman. But when I run my console application i cant catch the response data. Please someone guide how to do this? This is GPS data, providing API from one of our client, and i just want to consume the data and save the response from the API. I am passing vechile Numbers,start and end dates and getting the data in fields.
What I have tried:
static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://api.blabla.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
var dat = new APIEntity()
{
unitCodes = new List<int>() {19215,19216,...},
start = DateTime.ParseExact("2018-03-12 01:00:00", "yyyy-MM-dd HH:mm:ss", null),
end = DateTime.ParseExact("2018-03-12 23:00:00", "yyyy-MM-dd HH:mm:ss", null),
fields = new List<string>() { "unitCode", "timedate", "latitude", "longitude", "ignition", "velocity", "positionerror", "digitherm5", "direction", "distance" }
};
HttpResponseMessage postresponse = await client.PostAsJsonAsync("api/points", dat);
postresponse.EnsureSuccessStatusCode();
if (postresponse.IsSuccessStatusCode)
{
????????
}
}
catch (HttpRequestException e)
{
Console.WriteLine(e.Message);
}
}
}
static void Main()
{
RunAsync().Wait();
}
My Model is:
public class APIEntity
{
public List<int> unitCodes { get; set; }
public DateTime start { get; set; }
public DateTime end { get; set; }
public List<string> fields { get; set; }
}
For your reference : Postman Request is :
{
"unitCodes": [
19215,
19216,
19224
],
"start":"2018-03-20T09:00:00.000Z",
"end":"2018-03-20T15:10:00.000Z",
"fields": [
"unitCode",
"timedate",
"latitude",
"longitude",
"ignition",
"velocity",
"positionerror",
"digitherm5",
"direction",
"distance"
]
}
and Respose is :
{
"status": "Success",
"data": [
{
"unitCode": 19215,
"timedate": "2018-03-20 09:01:23",
"latitude": 25.216296552475924,
"longitude": 55.26109752059169,
"ignition": false,
"velocity": 0,
"positionError": false,
"seatCount": 0
},
{
"unitCode": 19215,
"timedate": "2018-03-20 09:11:23",
"latitude": 25.216199149650755,
"longitude": 55.261200652994816,
"ignition": false,
"velocity": 0.03,
"positionError": false,
"seatCount": 0
},
......
...............
}