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

Hello everyone,
I would like to ask for help in how to extract submission data from kobotoolbox using the API and c#.

I know how to access data from another website using API and c# but that website does not require logged in users.

My main problem is first, I do not know how to submit my user name and password so that I can start accessing the data via API.

I searched the community for examples to get me started but couldn’t find any. In Stackoverflow, I saw this but did not work for me.

That is why I am asking for examples from anyone who has done this already.

Thanks

Hi @raffy_m,

Have you gone through this post (it has a bunch of API endpoints which should solve your issue):

Have a great day!

1 Like

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

The c# code above works and gives me a json string representing an object hierarchy 3 levels deep.

Due to the naming convention used by kobotoolbox, a lot of the property names have embedded slashes in them like the example below.
"fishing_vessel_group/search_value":"m", "soak_time_group/soaktime_tracking_group/gps":"LAM"

I need to remove the embedded slashes from the property names before feeding the json string into a parser that creates the objects that represent one or more submissions.

I would like to ask if anyone already has a solution to this problem. I prefer a regex solution where it finds strings containing slashes that are enclosed in quotes. What I propose is to replace the slash with a safe character like 2 successive underscores so that the property name becomes legal in c#.

Thanks again.

1 Like

i have a problem with this api is, I can’t get my all data are I have submit please someone can help me

HI @mizanvai
Thank you for your message. Could you please provide more information about the problem? Kindly provide screenshots alongside a detailed narration of what part of the data is missing.

Stephane

thank you for your response

this is my kobo survey from
https://drive.google.com/file/d/1dCYKbGmVajhx0p0qBwBgzHCFH_IJXhns/view?usp=sharing

this is my menually downloaded data


and this is my api json data

in there so many data field is missing

my api is

using (var httpClient = new HttpClient())
{
string api_call = “------”;
using (var request = new HttpRequestMessage(new HttpMethod(“GET”), api_call))
{
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes(“username: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);
//var members = JsonConvert.DeserializeObject<List>(the_response);
//_customService.SendShelterDataList(members);
}
}

thanks in advance
mizan