Posting form attachments via ODK Central API

Success!
Haha, I know I've been talking to myself on here. But I figured out the solution for regularly updating form attachments in Central via API.

The main challenge was that I didn't understand that it's not possible to delete / post to the published form directly.

So the process is:

  1. Post the form again as a draft
  2. Post the attachments to the draft form
  3. Publish the form with a new version number (which can be supplied in the API call, no need to update the version number inside the form.

For those of you, like myself, who need explicit code, here is what worked for me:

  1. Post the .xlsx form as a draft
    curl --include
    --request POST
    --header "Authorization: Basic [BASE64_ENCODE_OF_email:password]"
    --header "Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    --header "X-XlsForm-FormId-Fallback: [FormID]"
    --data-binary "@[FILE_PATH_TO_FORM.xlsx]"
    'https://WEBSITE.com/v1/projects/[ProjectID]/forms/[FormID]/draft?ignoreWarnings=true'

  2. Post attachments to the form
    curl --include
    --request POST
    --header "Authorization: Basic [BASE64_ENCODE_OF_email:password]"
    --header "Content-Type: text/csv"
    --data-binary "@[FILE_PATH_TO_ATTACHMENT.csv]"
    'h ttps://WEBSITE.com/v1/projects/[ProjectID]/forms/[FormID]/draft/attachments/ATTACHMENT.csv'

  3. Publish form with updated version
    curl --include
    --request POST
    --header "Authorization: Basic [BASE64_ENCODE_OF_email:password]"
    'h ttps://WEBSITE.com/v1/projects/[ProjectID]/forms/[FormID]/draft/publish?version=NEW_VERSION'

8 Likes