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

2

8

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?

Roley answered 15/7, 2015 at 23:44 Comment(0)
D
3

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.

Dentilingual answered 22/1, 2021 at 18:0 Comment(0)
R
-4

It looks like some headers are set before the ResponseWriter is invoked which happens inside ProcessResponseStream method and some headers are set after. Status is one of those headers that is set after the call to ResponseWriter.

See code:RestSharp Http.cs

       private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse)
        {
            using (webResponse)
            {
#if FRAMEWORK
                response.ContentEncoding = webResponse.ContentEncoding;
                response.Server = webResponse.Server;
                response.ProtocolVersion = webResponse.ProtocolVersion;
#endif
                response.ContentType = webResponse.ContentType;
                response.ContentLength = webResponse.ContentLength;

                Stream webResponseStream = webResponse.GetResponseStream();

#if WINDOWS_PHONE
                if (string.Equals(webResponse.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase))
                {
                    GZipStream gzStream = new GZipStream(webResponseStream);

                    ProcessResponseStream(gzStream, response);
                }
                else
                {
                    ProcessResponseStream(webResponseStream, response);
                }
#else
                this.ProcessResponseStream(webResponseStream, response);
#endif

                response.StatusCode = webResponse.StatusCode;
                response.StatusDescription = webResponse.StatusDescription;
                response.ResponseUri = webResponse.ResponseUri;
                response.ResponseStatus = ResponseStatus.Completed;
Riffle answered 4/10, 2016 at 16:21 Comment(1)
This doesn't answer the question at all. What is your suggestion to fix it?Ajmer

© 2022 - 2024 — McMap. All rights reserved.