Upload form and media files to ODK Aggregate using R (httr)

Recently, I had a task to upload updated csv file to my Aggregate for ongoing survey form. I decided to automatize this task as the data for this csv file was collected from another survey that was going on in parallel and I had to update the csv file several time a day.

The first Idea was to use Briefcase's CLI but as documentation warns:

This CLI operation will only update the blank form if it does not already exist, whereas the GUI will always update the form.

Then I decided to use httr package of R. Here the code:

url <- "http://[MyAggregateURL]/formUpload"
myuser <- "MyAggregateUser"
mypass <- "MyPassword"
myform <- "MyForm.xml"
mydataA <- "MyFirstData.csv"
mydataB <- "MySecondData.csv"
POST(url, authenticate(myuser, mypass, type = "basic"),
body=list(name=myform, form_def_file=upload_file(myform, "xml"),
name=mydataA, datafile=upload_file(mydataA, "text/csv"),
name=mydataB, datafile=upload_file(mydataB, "text/csv")))

Hope this could be useful to anyone that want to upload a form using R.
PS. You should also increment the version of your form each time you upload an updated media file.