{"record":{"id":"2909336ba919142b","repo":"kataras/iris","slug":"compress-response-will-not-be-compressed","errorCode":null,"errorMessage":"compress: response will not be compressed","messagePattern":"compress: response will not be compressed","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"info","filePath":"context/compress.go","lineNumber":33,"sourceCode":")\n\n// The available builtin compression algorithms.\nconst (\n\tGZIP    = \"gzip\"\n\tDEFLATE = \"deflate\"\n\tBROTLI  = \"br\"\n\tSNAPPY  = \"snappy\"\n\tS2      = \"s2\"\n)\n\n// IDENTITY no transformation whatsoever.\nconst IDENTITY = \"identity\"\n\nvar (\n\t// ErrResponseNotCompressed returned from AcquireCompressResponseWriter\n\t// when response's Content-Type header is missing due to golang/go/issues/31753 or\n\t// when accept-encoding is empty. The caller should fallback to the original response writer.\n\tErrResponseNotCompressed = errors.New(\"compress: response will not be compressed\")\n\t// ErrRequestNotCompressed returned from NewCompressReader\n\t// when request is not compressed.\n\tErrRequestNotCompressed = errors.New(\"compress: request is not compressed\")\n\t// ErrNotSupportedCompression returned from\n\t// AcquireCompressResponseWriter, NewCompressWriter and NewCompressReader\n\t// when the request's Accept-Encoding was not found in the server's supported\n\t// compression algorithms. Check that error with `errors.Is`.\n\tErrNotSupportedCompression = errors.New(\"compress: unsupported compression\")\n)\n\n// AllEncodings is a slice of default content encodings.\n// See `AcquireCompressResponseWriter`.\nvar AllEncodings = []string{GZIP, DEFLATE, BROTLI, SNAPPY}\n\n// GetEncoding extracts the best available encoding from the request.\nfunc GetEncoding(r *http.Request, offers []string) (string, error) {\n\tacceptEncoding := r.Header[AcceptEncodingHeaderKey]\n","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/context/compress.go#L15-L51","documentation":"ErrResponseNotCompressed is returned by AcquireCompressResponseWriter (surfaced via GetEncoding) when Iris decides the response cannot or should not be compressed. This happens when the response's Content-Type header is missing (a known Go issue golang/go#31753) or when the client's Accept-Encoding header is empty. It is a sentinel signal for the caller to fall back to the original, uncompressed response writer rather than a failure.","triggerScenarios":"Calling AcquireCompressResponseWriter/GetEncoding when (1) the response has no Content-Type header set (Go strips it when WriteHeader has not decided content type, golang/go#31753), or (2) the request's Accept-Encoding header is empty or absent.","commonSituations":"Client tools like curl without --compressed, or non-HTTP/1.1 clients that omit Accept-Encoding; handlers that write the body before the Content-Type is determined; proxies or middleware that strip Content-Type; apps relying on Go's automatic content-type sniffing without calling ctx.ContentType first.","solutions":["Ensure the client sends an Accept-Encoding header (e.g. curl --compressed or a browser/fetch client).","Set the response Content-Type explicitly via ctx.ContentType(\"text/html\") or similar before acquiring the compress writer.","Treat the error as expected control flow: use errors.Is(err, iris.ErrResponseNotCompressed) and fall back to writing with the original ResponseWriter.","If using middleware (compress.New), verify it is configured with encodings the client supports."],"exampleFix":"// before\nw, err := ctx.CompressWriter(false)\nif err != nil { ctx.StopWithStatus(500); return }\n// after\nw, err := ctx.CompressWriter(false)\nif err != nil {\n    if errors.Is(err, iris.ErrResponseNotCompressed) {\n        // fall back to the original writer, no compression\n        ctx.ContentType(\"text/plain\")\n        ctx.WriteString(\"data\")\n        return\n    }\n    ctx.StopWithStatus(500)\n    return\n}","handlingStrategy":"fallback","validationCode":"acceptEnc := ctx.GetHeader(\"Accept-Encoding\")\ncontentType := ctx.GetHeader(\"Content-Type\") // response-side: ensure ctx.ContentType was called\nif acceptEnc == \"\" || contentType == \"\" {\n    // skip compression, use original writer\n}","typeGuard":"func willCompress(acceptEncoding, contentType string) bool {\n    return acceptEncoding != \"\" && contentType != \"\"\n}","tryCatchPattern":"w, err := ctx.CompressWriter(false)\nif err != nil {\n    if errors.Is(err, iris.ErrResponseNotCompressed) {\n        // fall back: write uncompressed with the original writer\n    } else {\n        ctx.StopWithStatus(iris.StatusInternalServerError)\n        return\n    }\n}","preventionTips":["Always set ctx.ContentType before acquiring the compress writer so Go does not strip Content-Type (golang/go#31753).","Never treat ErrResponseNotCompressed as a server failure; it is an expected control-flow sentinel.","Test clients both with and without Accept-Encoding (curl --compressed vs plain curl).","Centralize compression behind Iris compress middleware so fallback handling lives in one place."],"tags":["http","compression","iris","encoding"],"backgroundTag":"response-not-compressed","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}