9

Below, there is a POST Request Done With Standard HttpWebRequest , and HttpWebResponse. Basically it post binanry file with some parameters.

How Can I do The Same Thing With RestSharp ?

Source:

https://github.com/attdevsupport/ATT_APIPlatform_SampleApps/tree/master/RESTFul/SpeechCustom, ATT_APIPlatform_SampleApps/RESTFul/Speech/Csharp/app1/ ]

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(string.Empty+parEndPoint );

httpRequest.Headers.Add("Authorization", "Bearer " + parAccessToken);
httpRequest.Headers.Add("X-SpeechContext", parXspeechContext);
if (!string.IsNullOrEmpty(parXArgs))
{
    httpRequest.Headers.Add("X-Arg", parXArgs);
}
string contentType = this.MapContentTypeFromExtension(Path.GetExtension(parSpeechFilePath));
httpRequest.ContentLength = binaryData.Length;
httpRequest.ContentType = contentType;
httpRequest.Accept = "application/json";
httpRequest.Method = "POST";
httpRequest.KeepAlive = true;
httpRequest.SendChunked = parChunked;
postStream = httpRequest.GetRequestStream();
postStream.Write(binaryData, 0, binaryData.Length);
postStream.Close();

HttpWebResponse speechResponse = (HttpWebResponse)httpRequest.GetResponse();
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Hippias Minor
  • 1,917
  • 2
  • 21
  • 46

1 Answers1

11

A simple upload example:

RestClient restClient = new RestClient("http://stackoverflow.com/");
RestRequest restRequest = new RestRequest("/images");
restRequest.RequestFormat = DataFormat.Json;
restRequest.Method = Method.POST;
restRequest.AddHeader("Authorization", "Authorization");
restRequest.AddHeader("Content-Type", "multipart/form-data");
restRequest.AddFile("content", imageFullPath);
var response = restClient.Execute(restRequest);
Matix
  • 99
  • 2
  • 9
Heming Chen
  • 174
  • 2
  • 10
  • How do I do the same thing without multipart? Can you suggest the same way to do in ruby? – RV_Dev Dec 01 '17 at 21:21
  • 4
    This answer does not answer the question. It is explicitly asking, how it can be done without multipart/form-data. I have the problem that an API for uploading files responses with error "UnsupportedMediaType" if using this type. – maeni70 Jan 31 '20 at 08:25