Skip to content

Response is limited to 64kB when HTTP status isn't 200 #884

@Buthrakaur

Description

@Buthrakaur

I'm getting response truncated to 64kB when REST service returns response status different than "OK" (like "409 Conflict" in my case). Is there any option how to change or turn this limit completely off? This behavior is probably backed in HttpWebRequest, but I wasn't able to figure out exact place in the .NET source code where the response trim occurs.

Failing test:

        [Test]
        public void Test()
        {
            var cli = new RestClient("http://service/with/500kB/JSON/response/and/status/409");
            var response = cli.Post<dynamic>(new RestRequest
            {
                Resource = "xxx",
                RequestFormat = DataFormat.Json,
                Method = Method.POST,
                JsonSerializer = new RestSharpJsonNetSerializer()
            });
            var result = JsonConvert.DeserializeObject<dynamic>(response.Content); // => this throws because of JSON is truncated
        }

I tried to write the test on top of HttpWebResponse and the problem is same:

        [Test]
        public void HttpRequest()
        {
            var request = (HttpWebRequest) WebRequest.Create("http://service/with/500kB/JSON/response/and/status/409");
            request.Method = "POST";

            var rs = GetRawResponse(request);

            rs.StatusCode.Should().Be(HttpStatusCode.Conflict);
            var content = rs.GetResponseStream().ReadToEndSync();
            content.Length.Should().Be((int) rs.ContentLength); // => this throws Expected value to be 512000, but found 65294.
        }

        private static HttpWebResponse GetRawResponse(HttpWebRequest request)
        {
            try
            {
                return (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                if (ex.Response is HttpWebResponse)
                    return ex.Response as HttpWebResponse;
                throw;
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions