adiga
adiga

Reputation: 35222

"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

Upvotes: 1

Views: 1237

Answers (4)

HariHaran
HariHaran

Reputation: 4119

This is 2019 and i couldn't find a proper library for dotnet to consume the API's. So i created one myself. Still a long way to go. I am planning to maintaining it as long as i can.It have published it as nuget as well as you can have a look at the source here on Github

From Nuget

Install-Package StackExchange.NET -Version 1.1.0

Upvotes: 0

Todd Menier
Todd Menier

Reputation: 39319

UPDATE: Flurl.Http now supports automatic decompression by default, so just upgrade and you can avoid all of this.


Here's an option that resembles the accepted answer, except it only applies to calls to api.stackexchange.com, and it's more or less a one-liner. Call it once at startup:

FlurlHttp.ConfigureClient("https://api.github.com", cli => 
    ((HttpClientHandler)cli.HttpMessageHandler).AutomaticDecompression =
        DecompressionMethods.GZip | DecompressionMethods.Deflate);

As a side-note, Flurl's defaults are exactly the same as HttpClient's defaults in terms of automatic decompression. There are notes in the comment that GZIP works with HttpClient, which had me scratching my head, but it seems that with HttpClient there are differences depending on platform/version. To make things easier and more predictable, I am considering supporting GZIP/DEFLATE by default in Flurl, but I first need to understand the implications in terms of performance, etc.

Upvotes: 2

Todd Menier
Todd Menier

Reputation: 39319

Flurl.Http 2.2 was just released, and it will now automatically decompress GZIP and DEFLATE by default. So, the new best answer is: upgrade. :)

Upvotes: 1

DavidG
DavidG

Reputation: 118977

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();

Upvotes: 3

Related Questions