How can I send submissions via new API or other method

I’m trying to find a complete API documentation but without luck, until now I found the following approach:

Using the Kobo REST API (https://kc.kobotoolbox.org/api/v1/) that resource explain some endpoints

https://support.kobotoolbox.org/api.html explain how to get the access token and some endpoint usage

I need more detailed documentation,

I tried to use de old API but I got always the same 500 error message when I send a submission. Now trying to use the new I found the entry point: /assets/uid/submissions but in the documentation say:

**It's not allowed to create submissions with kpi 's API**

How can I send data to kobo, via curl or using some programming language like JavaScript or python?

Thanks so much for the help.

Hi @finlay,

Would you mind sharing the screenshot of the curl command that you had used along with a description of what you wish to do and the error message that you see at your end. Maybe the community could help you out if there is any typo in your curl command.

Have a great day!

Thanks @Kal_Lam , for your reply:

for API v1:

curl -X POST -F xml_submission_file=submissions.xml https://kc.domain.tld/api/v1/submissions -u user:secret

A long html with:

500 Error

for API v2:

curl -X POST  https://kf.domain.tld/assets/adpukpDhKvFEw8y6SRUtDv/submissions.json -H "Authorization: Token TOKENSTRING" -d "{'field':'Text'}"

{"detail":"Method \"POST\" not allowed."}
  • Looks like you might be missing an @ symbol before submissions.xml.
  • Since your submissions.xml has a plural filename, I advise you that xml_submission_file must contain only one submission. If you have multiple submissions, POST multiple times.
  • Try your command with kc.kobotoolbox.org (create a free account there if you haven’t already) before trying on your private instance (assuming you have one based on kc.domain.tld in your example). If you still receive a 500 error, we can review our logs to see why.
2 Likes

I fix it running:

curl -v -X POST -F "xml_submission_file=@submission.xml;type=text/xml" https://kc.domain.tld/api/v1/submissions -u user:pass

The sintaxis of submission.xml is:

<?xml version="1.0" ?>
<data id="formId" >
    <device_id>%s</device_id>
    <start_time>%s</start_time>
    <end_time>%s</end_time>
    <url>%s</url>
    <category>%s</category>
    <meta>
        <instanceID>formId</instanceID>
    </meta>
</data>

Thanks

2 Likes

Great, I’m glad it’s working for you. Some of your XML looks a bit odd; I’m not sure what type="str" is doing, and you really should include <meta><instanceID> (the specification requires it) even if KoBoCAT accepts a submission without it.

Here’s an example of an expert creating XML submissions programmatically. I reformatted the XML template he uses to be more readable and included it below.

<?xml version='1.0' ?>
<data id="covid-19_A0" version="2020032802" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:odk="http://www.opendatakit.org/xforms" xmlns:orx="http://openrosa.org/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<device_id>%s</device_id>
	<start_time>%s</start_time>
	<end_time>%s</end_time>
	<case>
		<case_id>%s</case_id>
		<status>%s</status>
	</case>
	<data_collector>
		<dc_name>%s</dc_name>
		<dc_institution>%s</dc_institution>
	</data_collector>
	<case_info>
		<sex>%s</sex>
	</case_info>
	<case_id_dob>
		<age_years>%s</age_years>
		<age_months>%s</age_months>
	</case_id_dob>
	<case_status>%s</case_status>
	<symptoms_1>
		<fever>%s</fever>
		<sore_throat>%s</sore_throat>
		<runny_nose>%s</runny_nose>
		<cough>%s</cough>
	</symptoms_1>
	<symptoms_2>
		<shortness_of_breath>%s</shortness_of_breath>
		<vomiting>%s</vomiting>
		<nausea>%s</nausea>
		<diarrhoea>%s</diarrhoea>
	</symptoms_2>
	<meta>
		<instanceID>%s</instanceID>
	</meta>
</data>
2 Likes

Yes the xml looks more than bit odd, thanks for the recommendations I’ll update the post.

2 Likes