Uploading Stream/Images to REST API Using RestSharp

While I was working with the REST API to upload an image using RestSharp, I encountered a problem uploading it. I Googled the problem but did not find a specific answer for it. Although I found the solution and thought to share it since it can save a lot of time for other developers. I am assuming here that the reader is familiar with the REST API and RestSharp.

  1. [OperationContract]  
  2. [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]  
  3.         public void NotesAttachment(Stream input)  
  4.         {  
  5.             HttpApplicationState Application = HttpContext.Current.Application;  
  6.             HttpRequest Request = HttpContext.Current.Request;  
  7.             string sRequest = String.Empty;  
  8.             using (StreamReader stmRequest = new StreamReader(input, System.Text.Encoding.UTF8))  
  9.             {  
  10.                 sRequest = stmRequest.ReadToEnd();  
  11.             }  
  12.             JavaScriptSerializer json = new JavaScriptSerializer();  
  13.             json.MaxJsonLength = int.MaxValue;  
  14.             Dictionary<string, object> dict = json.Deserialize<Dictionary<string, object>>(sRequest);  
  15.             string ext = dict["FILE_EXT"] as string;  
  16.             string mime = dict["FILE_MIME_TYPE"] as string;  
  17.             string image = dict["IMAGE_DATA"] as string;         byte[] attachment = Convert.FromBase64String(image);
  18.         }  
What this method (the WS method) is expecting is a Stream object containing a file extension, file mime type and file data as an encoded string that is being converted to a byte array.

JavaScriptSerializer used in the example above is System.Web.Script.Serialization from systems.web.extension.dll.

This is how it is called from the client, I am using WPF as the client, but it can be called from a console or web app.
  1. private void CallRestNotesAttachment(string ext, string mime)  
  2. {  
  3.       var restClient = new RestClient("http://localhost:1608/Rest.svc/NotesAttachment");  
  4.       restClient.AddHandler("application/octet-stream"new RestSharp.Deserializers.JsonDeserializer());  
  5.       var request = new RestRequest(Method.POST);  
  6.       request.AddHeader("Content-Type""application/octet-stream");  
  7.       Dictionary<string, object> dict = new Dictionary<string, object>();  
  8.       dict.Add("FILE_EXT", ext);  
  9.       dict.Add("FILE_MIME_TYPE", mime);  
  10.       byte[] imageBytes;  
  11.       using (FileStream fs = File.Open(@"C:\Users\anand.thakur\Desktop\1.wmv", FileMode.Open))  
  12.       {  
  13.           imageBytes = new BinaryReader(fs).ReadBytes((int)fs.Length);  
  14.       }  
  15.       string image = Convert.ToBase64String(imageBytes);  
  16.       dict.Add("IMAGE_DATA", image);  
  17.       byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(dict));  
  18.       request.AddParameter("application/octet-stream", data, ParameterType.RequestBody);  
  19.       var response = restClient.Execute(request);  
  20.       JavaScriptSerializer json = new JavaScriptSerializer();  
  21.       Dictionary<string, object> dict1 = json.Deserialize<Dictionary<string, object>>(response.Content);  
  22.       MessageBox.Show("done");  
  23.   }  
The RestSharp package is available through NuGet. JsonConvert can be found in Newtonsoft.Json, that is also available at NuGet.

 

Up Next
    Ebook Download
    View all
    Learn
    View all