Skip to content

Failing to handle gzipped responses #349

@vince-nyanga

Description

@vince-nyanga

When using the Mindee SDK in an ASP.NET Core application, API responses are GZIP-compressed but are not being automatically decompressed by RestSharp, causing JSON deserialization to fail with System.Text.Json.JsonException: '0x1F' is an invalid start of a value.

Expected Behavior

The RestClient configured in RegisterV2RestSharpClient should automatically decompress GZIP-encoded responses before passing them to DeserializeResponse.

Actual Behavior

API responses remain GZIP-compressed (starting with byte 0x1F 0x8B), causing deserialization to fail in HttpApiV2.DeserializeResponse<TResponse>(String responseContent).

Steps to Reproduce

  1. Create an ASP.NET Core application
  2. Install Mindee SDK
  3. Use MindeeClientV2 to call EnqueueAndGetInferenceAsync
  4. Observe JsonException when polling for results

Root Cause (I suspect)

The RestClientOptions in RegisterV2RestSharpClient does not set AutomaticDecompression:

var clientOptions = new RestClientOptions(settings.MindeeBaseUrl)
{
    FollowRedirects = false,
    Timeout = TimeSpan.FromSeconds(settings.RequestTimeoutSeconds),
    UserAgent = BuildUserAgent(),
    Expect100Continue = false,
    CachePolicy = new CacheControlHeaderValue { NoCache = true, NoStore = true },
    ThrowOnAnyError = throwOnError,
    // Missing: AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};

Stack Trace

System.Text.Json.JsonException: '0x1F' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpa`1 json, JsonTypeInfo`1 jsonTypeInfo)
   at Mindee.Http.HttpApiV2.DeserializeResponse[TResponse](String responseContent)
   at Mindee.Http.MindeeApiV2.ResponseHandler[TResponse](RestResponse restResponse)
   at Mindee.Http.MindeeApiV2.ReqGetJobAsync(String jobId)

Workaround (not pretty)

Created a custom MindeeApiV2 implementation that manually decompresses GZIP responses in the ResponseHandler method before deserialization.

private TResponse ResponseHandler<TResponse>(RestResponse restResponse)
    where TResponse : CommonResponse, new()
{
    int statusCode = (int)restResponse.StatusCode;

    if (statusCode is > 199 and < 400)
    {
        string content = restResponse.Content;
        
        // Check if response is GZIP compressed
        if (restResponse.RawBytes != null && 
            restResponse.RawBytes.Length > 2 && 
            restResponse.RawBytes[0] == 0x1F && 
            restResponse.RawBytes[1] == 0x8B)
        {
            using var compressedStream = new MemoryStream(restResponse.RawBytes);
            using var gzipStream = new GZipStream(compressedStream, CompressionMode.Decompress);
            using var decompressedStream = new MemoryStream();
            
            gzipStream.CopyTo(decompressedStream);
            content = Encoding.UTF8.GetString(decompressedStream.ToArray());
        }
        
        return DeserializeResponse<TResponse>(content);
    }

    throw new MindeeHttpExceptionV2(
        GetErrorFromContent((int)restResponse.StatusCode, restResponse.Content));
}

Additional Context

This issue only occurs in ASP.NET Core applications, not in console applications, suggesting an environmental difference in how RestSharp or HttpClient handles compression by default.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions