question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON parsing result for friends list or batching fails on UWP app

See original GitHub issue

Whenever I use the SDK for a UWP app, calling to get a friends list and/or making a batch call leads to JSON deserialization exception. This is likely due to these lines in FacebookClient.cs ~Line 614:

#if !(SILVERLIGHT || NETFX_CORE)
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            request.AllowWriteStreamBuffering = false;
#endif
#if NETFX_CORE
            request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip,deflate";
#endif

The problem here is that this code prevents automatic decompression but tells FB that it accepts compressed results.

I was able to get around this by overriding the HttpWebRequestWrapper and setting the web request factory to return that wrapper. In the TrySetUserAgent, I then clear the accept.

    public class MyHttpWebRequestWrapper : HttpWebRequestWrapper
    {
        public MyHttpWebRequestWrapper(HttpWebRequest request) : base(request)
        {
        }
        public override bool TrySetUserAgent(string userAgent)
        {
            this.Headers[HttpRequestHeader.AcceptEncoding] = "";
            return base.TrySetUserAgent(userAgent);
        }
    }

I could use the deserializationn handler to see that the results were not text let alone JSON. Fought with this for hours…

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
MrAndrewAucommented, Feb 29, 2016

For me, the SDK has recently stopped working in my Windows 10 Universal Project. Requests throw the “Invalid JSON” exception as the results looked like gibberish. I think we are experiencing the same problem.

However, the SDK still works fine in my Windows 8.1 project. I encountered a similar situation recently with the Twitter SDK where web requests worked in a Windows 8.1 project but not in a Windows 10 project, and it was because Windows 10 apps enforce HTTP/2 protocol (instead of HTTP/1.1). This caused things to break, despite Microsoft’s promise that nothing needs to be changed after the update.

It looks like we have to enforce protocol to HTTP/1.1, but the problem is that System.Net.HttpWebRequest in .NET Core doesn’t have a definition for ProtocolVersion.

1reaction
AndyDragoncommented, Feb 16, 2016

Here is the code to create and prepare the client:

        public static FacebookClient CreateClient()
        {
            var client = new FacebookClient(Session.ActiveSession.CurrentAccessTokenData.AccessToken);
            client.SetHttpWebRequestFactory(uri => {
                var request = new MyHttpWebRequestWrapper((HttpWebRequest)WebRequest.Create(uri));
                return request;
            });
            return client;
        }

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parsing Json in UWP
I'm working on UWP and I'm trying to parse this Json but can't understand why this code can't work please anyone can check...
Read more >
Error when trying to use output of Parse JSON
Error when trying to use output of Parse JSON · 1. I deleted and readded the create item (several times). · 2. I...
Read more >
JSON parser step
Identify structured data from a JSON payload without having to write a script. Map incoming JSON content to a complex object output that...
Read more >
JSON functions | BigQuery
Inserts JSON data into a JSON array. JSON_EXTRACT, (Deprecated) Extracts a JSON value and converts it to a SQL JSON-formatted STRING or JSON...
Read more >
What Is JSON and How to Handle an “Unexpected Token” ...
Learn what JSON is and how you can deal with errors occurring when parsing JSON data, such as "Unexpected Token < in JSON...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found