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.
- 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
- {
- // HTTP POST
- var dat = new APIEntity()
- {
- unitCodes = new List<int>() {19215,19216,...}, /
- / Multiple vehicle numbers
- start = DateTime.ParseExact("2018-03-12 01:00:00", "yyyy-MM-dd HH:mm:ss", null), // format : ISO 8601.
- end = DateTime.ParseExact("2018-03-12 23:00:00", "yyyy-MM-dd HH:mm:ss", null), // format : ISO 8601.
- fields = new List<string>()
- { "unitCode", "timedate", "latitude", "longitude", "ignition", "velocity", "positionerror", "digitherm5", "direction", "distance" } // I should get the values of the fields and save it to my database. };
- HttpResponseMessage postresponse = await client.PostAsJsonAsync("api/points", dat);
- postresponse.EnsureSuccessStatusCode(); // postrespones code 200 ok.
- if (postresponse.IsSuccessStatusCode)
- {
- ???????? // how to fetch the data?
- }
- }
- 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 }, ...... ............... }
I have a doubt, Since its a post request, how should I receive the response from the colsole application? Should i really need to write GET request? but they are providing only the POST method ? Is this possible to achieve this?
Ketan MokariyaPosted Mar 29, 2018, 6:50 AM
Anton NijusPosted Mar 29, 2018, 7:06 AM
Anton NijusPosted Mar 29, 2018, 6:17 AM
Ketan MokariyaPosted Mar 29, 2018, 4:46 AM