Search code examples
c#stackexchange-apiflurl

"Unexpected character exception" when making GET request to StackExchange API using Flurl


I have created a console application in which I'm making a simple GET request to the Stack Exchange API to fetch some comments. I'm using Flurl. This method is called from Main

private static async Task GetComments()
{
    dynamic d = await "https://api.stackexchange.com/2.2/comments?page=1&pagesize=5&order=desc&min=1513468800&max=1513555200&sort=creation&site=stackoverflow"
                        .GetJsonAsync();
}

But, I get this error:

{"Unexpected character encountered while parsing value: \u001f. Path '', line 0, position 0."}

I have tried setting the headers like this with no luck.

dynamic d = await new Url("https://api.stackexchange.com/2.2/comments.....")
               .WithHeader("Content-Encoding", "gzip")
               .WithHeader("Accept-Encoding", "gzip")
               .GetJsonAsync();

The URL does return proper JSON when I open it in the browser


Solution

  • So it seems the Flurl doesn't support Gzip out of the box and getting it to work takes a bit of massaging. First you need a custom Http Client factory:

    public class GzipClientFactory : Flurl.Http.Configuration.IHttpClientFactory
    {
        public HttpMessageHandler CreateMessageHandler() => new HttpClientHandler()
        {
            AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
        };
    
        public HttpClient CreateHttpClient(HttpMessageHandler handler) => 
            new HttpClient(handler);
    }
    

    Now configure Flurl to use this:

    FlurlHttp.Configure(settings => {
        settings.HttpClientFactory = new GzipClientFactory();
    });
    

    And now Gzip compression will be supported:

    dynamic d = await new Url("https://api.stackexchange.com/2.2/comments.....")
                   .GetJsonAsync();