Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autopatch PATCH request response body frequently has incorrect content encoding #753

Open
betaprior opened this issue Mar 8, 2025 · 1 comment
Labels
question Further information is requested

Comments

@betaprior
Copy link

betaprior commented Mar 8, 2025

I use Huma with the echo router via humaecho. I have echo gzip middleware enabled by default. This seems to not play well with autopatch, as follows: often, autopatch PATCH response unexpectedly comes through as gzipped when a plain text JSON is expected. This behavior was observed with a range of HTTP clients and a range of autopatched endpoints. The operation itself succeeds, returns 200, and performs the data changes expected, but the clients (expecting a JSON) barf on the binary response. A representative example from our API tests:

const response = await fetch('/test-endpoint', {
  method: 'PATCH',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ key: 'value' }),
});

const rawText = await response.clone().text();
console.log('Raw response:', rawText);  // supposed to be well-formed JSON but get binary gzipped data

Indeed, in autopatch.go, if you print the value of putWriter.Header().Get("Content-Encoding") you get gzip.

A way to work around it is to specify the "Accept-Encoding": "identity" header on the client side, however it seems that this is something that should not require additional headers. Because I don't understand the logic behind why the body comes through as gzipped in the case of autopatch and not with regular client-side GETs and PUTs, I am not sure what the correct fix is, but it would be nice to put it in place, or at the very least document that using a gzip middleware with autopatch requires the workaround above.

@betaprior betaprior changed the title Autopatch PATCH request response body is frequently corrupted Autopatch PATCH request response body frequently has incorrect content encoding Mar 9, 2025
@danielgtaylor danielgtaylor added the question Further information is requested label Mar 10, 2025
@danielgtaylor
Copy link
Owner

@betaprior thanks for reporting! I have not run into this, very interesting. I can definitely document it and will try to dig into the problem a bit to better understand it. Open to PRs if you want to submit one for the docs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants