How to upload files to restdb.io media archive using multipart form data

/* * C# example file upload to restdb.io * Using mulipart form data protocol */ byte[] data; // fill with some binary data string tmpId = ""; Uri webService = new Uri("https://somedb-c1c0.restdb.io/media"); HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, webService); requestMessage.Headers.ExpectContinue = false; MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----MyGreatBoundary"); ByteArrayContent byteArrayContent = new ByteArrayContent(data); byteArrayContent.Headers.Add("Content-Type", "application/octet-stream"); multiPartContent.Add(byteArrayContent, "profilePic", "profilePic.png"); requestMessage.Content = multiPartContent; requestMessage.Headers.Add("x-apikey", restDBApiKey); HttpClient httpClient = new HttpClient(); try { Task < HttpResponseMessage > httpRequest = httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead); HttpResponseMessage httpResponse = httpRequest.Result; HttpStatusCode statusCode = httpResponse.StatusCode; HttpContent responseContent = httpResponse.Content; if (responseContent != null) { Task < String > stringContentsTask = responseContent.ReadAsStringAsync(); String stringContents = stringContentsTask.Result; if (!stringContents.Substring(0, 1).Equals("[")) { string tmpStr = "[" + stringContents + "]"; stringContents = tmpStr; } var objects = JArray.Parse(stringContents); // parse as array foreach(JObject root in objects) { foreach(KeyValuePair < String, JToken > app in root) { if (app.Key.Equals("ids")) { tmpId = (String)app.Value[0]; } } } } } catch (Exception ex) { string errStr = ex.Message; }
Uploading media files to the restdb.io media archive can be challenging due to the multipart form-data HTTP protocol. This example shows how to do this using standard libraries from .NET.

RestDB.io docs here: https://restdb.io/docs/media-archive

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.