Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process RestSharp Response as a Stream after checking the response status?

I have to be able to stream large files from our Rest API using RestSharp. The canonical way of doing so is to set the 'ResponseWriter' property on the request:

var client = new RestClient
var request = new RestRequest();
IRestResponse response;
request.ResponseWriter = connectStream => {
  if(response.StatusCode == ResponseStatus.OK)
  {
     CloudStorage.UploadFromStream(connectStream);
  }
  else
  {
     LoggerService.LogErrorFromStream(connectStream);
  }
};
response = client.Execute(request);

My problem is that the 'response' object (which includes state, status code, headers, etc) isn't available until after RestSharp has finished asking my ResponseWriter to process the entire stream.

This seems counter-intuitive, since of course the user may want to change how a response stream is processed based on the response status.

How can I get this status information before I start processing the stream of the response body?

like image 684
Alain Avatar asked Jul 15 '15 23:07

Alain


1 Answers

I think this has been reported before here.

Looks like they released an update for this and the way to do it is to use the AdvancedResponseWriter instead.

var client = new RestClient
var request = new RestRequest();
IRestResponse response;
request.AdvancedResponseWriter = (stream, response) => {
    // Should be able to access response which is an IHttpResponse
};
response = client.Execute(request);

Docs.

like image 115
Jimenemex Avatar answered Sep 21 '22 14:09

Jimenemex