Click here to Skip to main content
15,791,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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("");
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    // 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)


     static void Main()

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": [
"fields": [

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
Updated 29-Mar-18 0:05am

1 solution

I googled "httpclient postasjsonasync example" for you and here is one of the results, feel free to google yourself to find more.

Call a Web API From a .NET Client (C#) | Microsoft Docs[^]

You'll need a class that represents the data you get back from the API, but as we don't know what data you get back we can't really help you you with that bit.
Share this answer
Member 13315484 29-Mar-18 7:19am    
I have got the response data.
{"status":"Success","data":[{"unitCode":19215,"timedate":"2018-03-12 10:09:48","latitude":25.215798079194162,"longitude":55.260702179713057,"ignition":false,"velocity":0.03,"positionError":false,"seatCount":0},{"unitCode":19215,"timedate":"2018-03-12 10:19:48","latitude":25.215901211597288,"longitude":55.260702179713057,"ignition":false,"velocity":0.07,"positionError":false,"seatCount":0},{"unitCode":19215,"timedate":"2018-03-12 10:29:48","latitude":25.215901211597288,"longitude":55.260702179713057,"ignition":false,"velocity":0.35,"positionError":false,"seatCount":0},{"unitCode":19215,"timedate":"2018-03-12 10:39:48","latitude":25.215798079194162,"longitude":55.260702179713057,"ignition":false,"velocity":0.03,"positionError":false,"seatCount":0},{"unitCode":19215,"timedate":"2018-03-12 10:49:48","latitude":25.215798079194162,"longitude":55.260702179713057,"ignition":false,"velocity":0.07,"positionError":false,"seatCount":0},{"unitCode":19215,"timedate":"2018-03-12 10:59:48","latitude":25.215798079194162,"longitude":55.260702179713057,"ignition":false,"velocity":0.27,"positionError":false,"seatCount":0}],"token":""}

Now i have few doubs.
1.How do i get these values and save it?
2.Should i need to deserialize the above json format?
F-ES Sitecore 29-Mar-18 7:22am    
You should deserialise the data into objects so you can use it. Google "c# deserialise json" as there are numerous ways to do this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900