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.

Gzip response is not being decompressed automatically

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi.middleware.gzip import GZipMiddleware
from fastapi import FastAPI
app = FastAPI(
    title="xyz",
    description="",
    version="1.0.0",
)

app.add_middleware(GZipMiddleware)

@app.get("/status", status_code=200, tags=["Health Check"])
async def health_check(request: Request):
    logger.info(f"health check was requested from {request.client.host}")
    return

Description

Hi,

I am trying to compress the data given from our API, as it sometimes include very large JSON data sets. i was looking into the built-in GZIP middleware .

I am not sure if what i encounter is an issue with FastAPI or something that i’m doing wrong, because when using some other endpoints that have GZIP response (like https://postman-echo.com/gzip) the response is being decompressed automatically both from postman and the browser itself.

Also, when trying to look for it online I could not find anything, which makes me believe something i do is wrong.

when trying to access the health check endpoint, i get the following response (should be stringified null): image

Same result is being received in the browser itself.

Am i missing something?

Thanks in advance.

Operating System

Linux, macOS

Operating System Details

No response

FastAPI Version

0.63.0

Python Version

3.8.0

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:17

github_iconTop GitHub Comments

1reaction
ghostcommented, May 21, 2022

Ah finally found the issue, I was using app.mount to mount the same app at the ‘/api’ endpoint.

Like this app.mount('/api', app) this might have messed up something removing this mount point fixed the issue, thanks!

1reaction
rafsafcommented, May 21, 2022

Yup, I see.

@JoshYuJump has fair point that if it doesn’t work and “something is no yes” with bundled Gzip Middleware or compression at all in FastAPI without clear reason (at least for three of us), then using custom based on build-in can be a nice alternative.

But it would be nice if we know that reason and create issue in this project or related one or document it if it is kind of expected and it actually works.

Have a nice day then! 😛

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is GZIP Automatically Decompressed by Browser?
All browser based requests (e.g., XHR/AJAX/jQuery, regular requests) will automatically be decompressed without additional effort from you. The browser is the ...
Read more >
Chapter 29. GZIP Compression/Decompression - JBoss.org
The client framework can (though not by default - see below) automatically set the Accept-Encoding header to be "gzip, deflate" so you do...
Read more >
gzip — Support for gzip files — Python 3.11.1 documentation
This module provides a simple interface to compress and decompress files just ... for filename being a file object, support for text mode,...
Read more >
Gitlab::HttpIO does not decompress responses sent with ...
This error is caused by the chunk of metadata.gz received being gzipped a second time by StorageGrid. Gitlab::Ci::Build::Artifacts::Metadata# ...
Read more >
How to Enable GZIP Compression to Speed Up WordPress Sites
Apart from being a data compression algorithm, GZIP is also a file extension (.gz) and a software used for file compression/decompression.
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