简体   繁体   中英

How do I upload document by converting HttpClient request to RestSharp request?

After i logged in to API i used, I am trying to upload a document with using RestSharp.

I was able to do this using Postman. Screenshot is given below:

邮递员请求成功。

Postman code generator generates this code block for me:

var client = new RestClient("http://80.211.238.187/AcumaticaERP/entity/Default/17.200.001/StockItem/POSTMAN123/files/sample.pdf");
client.Timeout = -1;
var request = new RestRequest(Method.PUT);
request.AddHeader("Content-Type", "application/pdf");
request.AddParameter("application/pdf", "<file contents here>", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

but idk how to edit the < file contents here > part from given code block above.

I was also able to achieve it using the HttpClient suggested in the respective API's documentation guide:

using (StreamReader sr = new StreamReader(@"C:\Users\fcomak\Downloads\sample.pdf"))
{
    _httpClient.PutAsync("http://80.211.238.187/AcumaticaERP/entity/Default/17.200.001/StockItem/POSTMAN123/files/sample.pdf", new StreamContent(sr.BaseStream))
        .Result
        .EnsureSuccessStatusCode();
}

But i need to convert HttpClient request to RestSharp request. I tried to achieve this using RestSharp, but failed. Even if I could send the file, its content was not browse correctly. Here is the code block i tried:

using (StreamReader sr = new StreamReader(@"C:\Users\fcomak\Downloads\sample.pdf"))
{
    restClient.Execute(new RestRequest("http://80.211.238.187/AcumaticaERP/entity/Default/17.200.001/StockItem/POSTMAN123/files/sample.pdf", Method.PUT)
        .AddHeader("Content-Type", "application/pdf")
        .AddParameter("application/pdf", new StreamContent(sr.BaseStream), ParameterType.RequestBody));
}

I can sending or getting json contents by using this api with this restClient by the way. And of course i logged in already. The reason for my failure is about my restClient. I would be grateful for any help.

I just solved the problem.

using (WebClient wc = new WebClient())
{
    byte[] value = wc.DownloadData("http://www.africau.edu/images/default/sample.pdf");
    restClient.Execute(new RestRequest("http://80.211.238.187/AcumaticaERP/entity/Default/17.200.001/StockItem/POSTMAN123/files/sample.pdf", Method.PUT)
        .AddHeader("Content-Type", "application/pdf")
        .AddParameter("application/pdf", value, ParameterType.RequestBody)
        );
}

As you see, i used WebClient.DownloadData() instead of StreamReader.BaseStream()

Or, you can convert you local data to byte array with File.ReadAllBytes()

string entitySource = @"C:\Users\fcomak\Downloads\sample2.pdf";
byte[] value = File.ReadAllBytes(entitySource);
restClient.Execute(new RestRequest(entityBasePath + entityName + "/" + inventoryID + "/files/" + fileName, Method.PUT)
        .AddHeader("Content-Type", "application/pdf")
        .AddParameter("application/pdf", value, ParameterType.RequestBody)
        );

FYI

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM