I was able to solve it.
@Josh posted this curl
script earlier:
curl https://<kc_url>/api/v1/metadata.json -H “Authorization: Token ”
-F “xform=<xform_id>”
-F “data_value=”
-F “data_type=media”
-F “data_file=@<path/to/file>”
I used this online C# to curl converter and this is the code i got:
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "http://https//<kc_url>/api/v1/metadata.json"))
{
request.Headers.TryAddWithoutValidation("Authorization", "Token <token>");
var multipartContent = new MultipartFormDataContent();
multipartContent.Add(new StringContent(File.ReadAllText("xform_id>")), "xform");
multipartContent.Add(new StringContent(File.ReadAllText("filename>")), "data_value");
multipartContent.Add(new StringContent("media"), "data_type");
multipartContent.Add(new ByteArrayContent(File.ReadAllBytes("<path/to/file>")), "data_file", Path.GetFileName("<path/to/file>"));
request.Content = multipartContent;
var response = await httpClient.SendAsync(request);
}
}
Some lines did not translate well:
multipartContent.Add(new StringContent(File.ReadAllText("xform_id>")), "xform");
multipartContent.Add(new StringContent(File.ReadAllText("filename>")), "data_value");
These should have been
multipartContent.Add(new StringContent("xform_id>"), "xform");
multipartContent.Add(new StringContent("filename>"), "data_value");
This is the working code that I use now:
private async Task<HttpResponseMessage> UploadMediaToServer(FileInfo file)
{
HttpResponseMessage result = null;
string baseURL = "https://kc.kobotoolbox.org/api/v1/metadata.json";
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), baseURL))
{
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_user}:{_password}"));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
var multipartContent = new MultipartFormDataContent();
multipartContent.Add(new StringContent(_formID), "xform");
multipartContent.Add(new StringContent(file.Name), "data_value");
multipartContent.Add(new StringContent("media"), "data_type");
multipartContent.Add(new StringContent("text/csv"), "data_file_type");
multipartContent.Add(new ByteArrayContent(File.ReadAllBytes(file.FullName)), "data_file", file.Name);
request.Content = multipartContent;
result = await httpClient.SendAsync(request);
}
}
return result;
}
The status code returned is 201, meaning it works perfectly.
Requerying the form metadata, the `text/csv’ attribute of the updated CSV file is not recognized as shown by the JSON text below:
{"url":"https://kc.kobotoolbox.org/api/v1/metadata/1331767?format=json",
"id":1331767,
"xform":<xformID>,
"data_value":"enumerator_select.csv",
"data_type":"media",
"data_file":"https://kobocat-s3.s3.amazonaws.com/<form_username>/form-media/<long random string>",
"data_file_type":"", CSV ATTRIBUTE IS NOT RECOGNIZED
"file_hash":"md5:be30dc27789387659818e428b63d867a"},
This is the json text of a csv file that was not updated. The text/csv
value is still there.
{"url":"https://kc.kobotoolbox.org/api/v1/metadata/1299805?format=json", "id":1299805,
"xform":<xformID>,
"data_value":"gear.csv",
"data_type":"media",
"data_file":"https://kobocat-s3.s3.amazonaws.com/<form_username>/form-media/<long random string>",
"data_file_type":"text/csv",
"file_hash":"md5:e89a4c6e61e958a5e7b3a624fc7fa8f1"},
Is the missing attribute something that should be of concern?
Thanks