Anton Nijus

Anton Nijus

  • NA
  • 25
  • 1.6k

How do i consume web API from console application ?

Mar 29 2018 3:57 AM
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.
  1. static async Task RunAsync()  
  2. {  
  3. using (var client = new HttpClient())  
  4. {  
  5. client.BaseAddress = new Uri("http://api.blabla.com/");  
  6. client.DefaultRequestHeaders.Accept.Clear();  
  7. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  8. try  
  9. {  
  10. // HTTP POST  
  11. var dat = new APIEntity()  
  12. {  
  13. unitCodes = new List<int>() {19215,19216,...}, /  
  14. / Multiple vehicle numbers  
  15. start = DateTime.ParseExact("2018-03-12 01:00:00""yyyy-MM-dd HH:mm:ss"null), // format : ISO 8601.  
  16. end = DateTime.ParseExact("2018-03-12 23:00:00""yyyy-MM-dd HH:mm:ss"null), // format : ISO 8601.  
  17. fields = new List<string>()  
  18. "unitCode""timedate""latitude""longitude""ignition""velocity""positionerror""digitherm5""direction""distance" } // I should get the values of the fields and save it to my database. };  
  19. HttpResponseMessage postresponse = await client.PostAsJsonAsync("api/points", dat);  
  20. postresponse.EnsureSuccessStatusCode(); // postrespones code 200 ok.  
  21. if (postresponse.IsSuccessStatusCode)  
  22. {  
  23. ???????? // how to fetch the data?  
  24. }  
  25. }  
  26. catch (HttpRequestException e)  
  27. {  
  28. Console.WriteLine(e.Message);  
  29. }  
  30. }  
  31. }  
  32. static void Main()  
  33. {  
  34. RunAsync().Wait();  
  35. }  
My Model is:
  1. public class APIEntity  
  2. {  
  3. public List<int> unitCodes { getset; }  
  4. public DateTime start { getset; }  
  5. public DateTime end { getset; }  
  6. public List<string> fields { getset; }  
  7. }  
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?

Attachment: Post-200ok.rar

Answers (4)