Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a file received as a binary content in the HTTP body using RestSharp in it's associated editor

Tags:

rest

c#

restsharp

As per the requirement, I've to call the REST service which will send back an attachment or a file as a binary content of the file in the HTTP body and the appropriate value of the content-type header and i need to open the contents in it's specific editor.

I'm using RestSharp and below is the code for the same: For eg. if the attachment is .doc-

var client = new RestClient(Properties.Settings.Default.RestServicesUrl + Constants.RestTicketServices);
var restRq = new RestRequest(Constants.SearchTicket);
restRq.Method = Method.GET;
restRq.RequestFormat = DataFormat.Json;
restRq.AddParameter("account_id", "10");
client.Authenticator = new HttpBasicAuthenticator(Properties.Settings.Default.RestAuthenticationUserName, Properties.Settings.Default.RestAuthenticationPass);
IRestResponse rest = client.Execute(restRq);

So, after this call:

rest.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=utf-8"
rest.Content = has binary data for the word document

and so on....

Basically, I'm looking how i can read the binary contents and open the same in the associated editor.

.xls - Excel .doc - Word .pdf - Adobe and so on.....

like image 766
user1871863 Avatar asked Oct 22 '25 06:10

user1871863


1 Answers

It's one year late, but recently i've searched answer for OP question and found it - just replace last line of your code:

byte[] bytes = client.DownloadData(restRq);

If You want to have also an IRestResponse, then You can get RawBytes by:

IRestResponse rest = client.Execute(restRq);
byte[] bytes = rest.RawBytes;

More interesting methods explained in RestSharp source.

like image 120
Sylwester Zarębski Avatar answered Oct 24 '25 19:10

Sylwester Zarębski