Malformed JSON when calling .json() on my response - Works in Postman

I have a login function that has been working perfectly for weeks.

My pages project called an external Worker that handles everything user based as far as getting and updating the user records.

The only thing I did Cloudflare related in my Pages project today was update Wrangler from 3.22.4 to the latest version 3.53.1

I loaded up my local dev servers to do some work but not my function that gets the user from the database is returning jibberish when I call the json method on the reponse.

I’ll show you the text being returned below. Here is more context.

  • The response is a 200.
  • The get user function works in Postman and returns the JSON data from the database
  • I reproduced my function call in a test endpoint in my Pages project and I get the jibberish

It definitely seems related to the Pages project.

The fetch call I make to my User Worker is just a normal fetch call in development env. Nothing fancy.

Here is the jibberish response I get when I call the .text() method on the reponse:

"\u0015R\u0001\u0000dA���]��9�\u00050Y�B���lL\u0012M)?Q@IZ\u0017\u0014)rs~�}4Ä���_������\u0002\u0015:��\u0003\u0002DP�y�hXF?,�'W|��ku*��@�?c?���3�IcM�<�Y�$��)($/�L�\u0016˓��!k�\u0006��*�3\u001b\u0010Pg\u0001\u0015�\u0001\"�(���5����*��\t��8��c����avC�@2�s�*\"��\u0017 �x$q1\u0003>\u0015��j��Vt,+vZ\u0002���\u0015���y�;\u0012\u0004�\u001f"

The date in request is correct.

Again besides normal code updates to the app… the only thing I did was upgrade Wrangler?

Any ideas?

Can I install the older version of Wrangler again without any crazy consequences?

I am using a global Wrangler command and not using the Wrangler in the project package.json.

This looks like encoding issues to me.

Do you have brotli enabled on the Pages zone (if it’s a custom domain)? What happens if you add a accept-encoding: gzip (omitting brotli) header on the outgoing request?

Or if you add the no_brotli_content_encoding compat flag to the worker / pages project?

@cherryjimbo holy ■■■■. You seemed to have saved me.

I added the accept-encoding: gzip to the outgoing request and it worked.

Can you explain to me why all of a sudden this happened?

Is this something I will need to add to all my requests?

Thank you so much for your help. I was stuck in developer depression. Ha!

Brotli changes have been rolling out for Cloudflare for a while, some references:

Did you update your compatibility_date in your wrangler.toml? Or are you not specifying one with your pages dev command? The brotli encoding flag was made default as of 2024-04-29, which it sounds like your project is now using, and causing this issue.

In a perfect world, brotli would just work everywhere, but there’s been a lot of edge-cases sadly.

1 Like

Oh cool. Thanks for the reading material. I will go through those for sure.

Yeah it must be my compatibility date. I don’t use a wranger.toml file in my Pages project… yet. I do want to convert to using one though.

I am going to read up. Thank you again for the help!!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.