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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With