R code for downloading a form's submissions in Central using API

1. What is the problem? Be very detailed.

I'm trying to create an R script that will download all the submissions for a form from ODK central into a local zip file with the CSV and media attachments.

I am using the ruODK package and httr. However, with the code I've tried below, I can't see how to download the data. Any help would be useful.

2. What app or server are you using and on what device and operating system? Include version numbers.

ODK Central v1.1.2

3. What you have you tried to fix the problem?

>  x <- httr::RETRY("GET",
>                 paste0(ruODK::get_default_url(), 
>                        "/v1/projects/",
>                        ruODK::get_default_pid(), 
>                        "/forms/", 
>                        "laLmRDT2", 
>                        "/submissions.csv.zip?media=true"),
>                 httr::authenticate(Sys.getenv("ODKC_UN"), 
>                                    Sys.getenv("ODKC_PW")
>                                    )) 

4. What steps can we take to reproduce the problem?

5. Anything else we should know or have? If you have a test form or screenshots or logs, attach below.

When this code is run I get the following message in the console window

Response [https:/xxx.xxx.xxx/laLmRDT2/submissions.csv.zip?media=true]
  Date: 2021-03-13 21:41
  Status: 200
  Content-Type: application/zip
  Size: 832 B

The ODK Central API lets you download all the submissions for a form into a zip file with the CSV and media attachments. No external libraries required. I think you should be able to use the Central API URL and the base R function download.file as described here.

Exporting Form Submissions to CSV
To export all the Submission data associated with a Form, just add .csv.zip to the end of the listing URL. The response will be a ZIP file containing one or more CSV files, as well as all multimedia attachments associated with the included Submissions.


1 Like

Hi @Lal_S,

This HTTP response indicates that you have received a binary file of mimetype "application/zip".
You are just missing the httr::write_disk argument to httr::RETRY/GET to actually save the file to disk instead of receiving the binary stream directly. See how ruODK does that here.

You should however not have to run httr::RETRY/GET yourself - the ruODK function to download submissions as zip archive (with or without nested tables or attachments - new!) submission_export does that for you.
See the submission_export examples and this vignette for more context and worked examples.