-1

I want to write a simple desktop application that issues GET requests to a REST API and parse the retrieved JSON into class objects.

This is the GET request I'm mainly gonna be using:

GET /api/timesheets (Returns a collection of timesheet records) GET /api/timesheets

Demo response

[
  {
    "activity": 174,
    "project": 12,
    "user": 1,
    "tags": [],
    "id": 5610,
    "begin": "2021-05-28T01:44:00-0700",
    "end": null,
    "duration": null,
    "description": null,
    "rate": 0,
    "internalRate": 0,
    "exported": false,
    "billable": true,
    "metaFields": []
  },
  {
    "activity": 305,
    "project": 23,
    "user": 1,
    "tags": [
      "Et quo.",
      "Aut ut."
    ],
    "id": 240,
    "begin": "2021-05-25T23:33:00-0700",
    "end": "2021-05-26T05:18:00-0700",
    "duration": 20700,
    "description": "Latitude was, or Longitude either, but thought they were mine before. If I or she should chance to be patted on the.",
    "rate": 465.75,
    "internalRate": 0,
    "exported": false,
    "billable": true,
    "metaFields": []
  }
]

I'm most interested in the user and duration values as I need to parse them into a Timesheet class object and store them inside a database.

So my question is: What's the best-practise way of doing this?

I've tried Process.Start() using the given curl and redirecting the StandardOutput to a string variable. This technically works but would be a pain and I'm sure there's a way better solution to this.

Thanks in advance!

SevenSins
  • 63
  • 5

1 Answers1

1

You could use the HttpClient API to issue a REST request and then parse the response directly in your .NET app:

HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("X-AUTH-USER", "susan_super");
httpClient.DefaultRequestHeaders.Add("X-AUTH-TOKEN", "api_kitten");
string json = await httpClient.GetStringAsync("https://demo.kimai.org/api/timesheets?user=1");

There is no need to start another process to do this.

How can I parse JSON with C#?

mm8
  • 163,881
  • 10
  • 57
  • 88