Examples in c# of getting data from kobotoolbox using the API

Thanks. I was able to get what I needed.

This is essentially a solution to the problem I posted regarding downloading submissions as an xls file.

So I explore the API docs and i found this example:
curl -X GET 'https://kc.kobotoolbox.org/api/v1/data/22845?query={"date": {"gt$": "2014-09-29T01:02:03+0000"}}'

I came across this post that clarified this example:

So the greater than operator is not gt$ but $gt and to get submissions past a certain date, you need to compare it with the _submission_time column.

So here is my code in c#, still raw, but it gets the work done. This effectively reduces the download size because you can formulate your query so it does not contain submissions that were already downloaded.

The string variable the_response contains the json string containing submissions that satisfy the condition _submission_time > 2020-08-19

                using (var httpClient = new HttpClient())
                {
                    string api_call = "https://kc.kobotoolbox.org/api/v1/data/[form_id]?format=json&query={\"_submission_time\" : {\"$gt\": \"2020-08-19\"}}";
                    using (var request = new HttpRequestMessage(new HttpMethod("GET"), api_call))
                    {
                        var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("user_name:password"));
                        request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
                        var response = await httpClient.SendAsync(request);
                        var bytes = await response.Content.ReadAsByteArrayAsync();
                        Encoding encoding = Encoding.GetEncoding("utf-8");
                        string the_response = encoding.GetString(bytes, 0, bytes.Length);
                    }
                }

Thanks to the community.

2 Likes