1

I am trying to send parameters & file in Request Body with Origin Request Header C# Console / Asp.Net (Web Form) Web Application. I have looked around many examples but I didn't get proper solutions.

I've tried with the following code which is almost working. The only issue with couldn't get any solution to send the file in Request Body.

 public class requestObj
        {
            public string langType { get; set; }
        }

protected void CreateUser_Click(object sender, EventArgs e)
        {
            try
            {           
                var requestObj = new requestObj
                {
                    langType = "aaa"
                };

                var client = new RestSharp.RestClient(url);
                client.Timeout = -1;
                var request = new RestSharp.RestRequest(RestSharp.Method.POST);
                request.AddHeader("Origin", "http://localhost:8080");
                request.AddJsonBody(requestObj);
                var response = client.Execute(request);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Thanks,

Nimesh
  • 3,342
  • 1
  • 28
  • 35
  • 1
    Check this https://stackoverflow.com/questions/16416601/c-sharp-httpclient-4-5-multipart-form-data-upload – user1672994 Aug 03 '20 at 16:24
  • Thanks for your response... I will check sure check this.. but can we do using WebClient ? Do you have any idea about this ? – Nimesh Aug 04 '20 at 04:54

1 Answers1

0

I have made it working following ways,

string url = requestUrl;
string filePath = @"F:\text.txt";


 var client = new RestSharp.RestClient(url);
                    client.Timeout = -1;
                    var request = new RestRequest(Method.POST);
                    request.AddHeader("Origin", "http://localhost:2020");
                    request.AddParameter("parameter", "parameterValue");
                    request.AddFile("file", filePath, "text/plain"); //file parameter.
                    IRestResponse response = client.Execute(request);
                    Console.WriteLine(response.Content);
Nimesh
  • 3,342
  • 1
  • 28
  • 35