How to post data to kobotoolbox form using the Kobo API?

1.7k views Asked by At

I'm working with the kobo REST API and I can get the structure's information from the form and the submitted data through the API, but when I try to send data to the form through a POST service, kobo returns the following message: "Your data for instance None has been already submitted.".

Is there any way to send POST data to a kobotoolbox form using its API?

I tried to send the data with the following POST request:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \
  "name": "Foo" \
}' 'https://kf.kobotoolbox.org/assets/{form_id}/submissions/'

Where {form_id} is the id from my form in kobotoolbox

And the response was:

{
  "detail": "Your data for instance None has been already submitted."
}
1

There are 1 answers

2
tinok On

To submit data you need to POST to https://[kobocat url]/api/v1/submissions, e.g. https://kc.kobotoolbox.org/api/v1/submissions. The endpoint you tried is for retrieving submitted data.

This will work (for a form that contains a first_name and last_name question:

curl -X POST \
  https://kc.kobotoolbox.org/api/v1/submissions.json \
  -H 'Authorization: Token <yourAPItoken>' \
  -H 'Content-Type: application/json' \
  -d '    {
        "id": "<kpi asset ID>",
        "submission":

            {
                "last_name": "Suresh",
                "first_name":"Ameen",
                "meta": {
                    "instanceID": "uuid:8992c3c2-3418-4ba9-a8e2-b00e03ea36b6"
                }

            }
    } '

<kpi asset ID> in this case refers to the unique form ID created in kobocat. This ID should be the same as the kpi asset ID, as long as you uploaded/created your form in the regular interface and deployed it.

You can find the ID by going to https://kf.kobotoolbox.org/assets/<kpi asset ID>/. In my case I get this:

{
    "url": "https://kf.kobotoolbox.org/assets/aCNoKPdCX8gqcu4bHrHkUL/",
...
    "deployment__identifier": "https://kc.kobotoolbox.org/<username>/forms/aCNoKPdCX8gqcu4bHrHkUL",
...
    "uid": "aCNoKPdCX8gqcu4bHrHkUL",

    ]
}