none
Couldn't able to send file through RestClient web service RRS feed

  • Question

  • Hello,

    I am trying to send a post request to webservice through rest client using multipart form data but the file is not being sent properly to the web service.Its sending the value like FileStream value but not all the contents of file. I could able to test using Postman and it works. Not sure how to stream the file content like multipart in rest request through c#. Also tried the same with HttpWebRequest but system is throwing 404 error. Webservice accepts only multipart data with key filename(test.xslx) and content(file contents) through paramters. Not sure how to call httpwebrequest with multipart form data parameters. Any help is greatly appreciated.

     var client = new RestClient("url");
                var request = new RestRequest(Method.POST);
                
                request.AddHeader("Cache-Control", "no-cache");
                request.AddHeader("Authorization", "Authtoken");
                request.AddHeader("Accept", "application/json");
                request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
                FileStream fs = File.OpenRead(@"C:\Users\testfile.xlsx");
                request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Dis name=\"filename\"\r\n\r\ntestfile.xlsx\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Dis name=\"content\"; filename=\""+ "testfile.xslx" + "\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n\r\n "+ filestream+"\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
           
         //     request.AddFile("content", @"C:\Users\testfile.xslx");
                IRestResponse response = client.Execute(request);
                Console.WriteLine(response.Content);
                Console.Read();

    • Moved by Fei Hu Monday, April 9, 2018 5:28 AM Moved Fron C#
    Saturday, April 7, 2018 5:45 PM

All replies

  • Sending multi-part through a Restful Web service can be discussed at the ASP.NET forums.

    https://forums.asp.net/28.aspx/1?WCF+ASMX+and+other+Web+Services

    Sunday, April 8, 2018 12:59 AM
  • Hi Swetha,

    According to your question is more related to ASP.NET, you could repost a new thread to Asp.Net forum for better support.

    If you have some grammar or code errors in using C#, please feel free to contact us. We will try our best to give you a solution.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei Hu Monday, April 9, 2018 5:28 AM wrong discussion
    Monday, April 9, 2018 5:27 AM