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

🤗 [Question]: How to get the RAW body? #2085

Closed
3 tasks done
alanhe opened this issue Sep 13, 2022 · 2 comments · Fixed by #2555
Closed
3 tasks done

🤗 [Question]: How to get the RAW body? #2085

alanhe opened this issue Sep 13, 2022 · 2 comments · Fixed by #2555

Comments

@alanhe
Copy link

alanhe commented Sep 13, 2022

Question Description

I want to support multi- Content-Encoding.

I find the method Body() that is supposed to give me the raw body. But when I check the source code, it seems to decompress some encodings by default.

For example, when it is gzip, I can call Body() to get the decompressed body, while it is snappy, I will have to handle the decompression logic myself.

Could you teach me if there's a better way to handle decompression?

fiber/ctx.go

Lines 281 to 311 in 8ec62a6

// Body contains the raw body submitted in a POST request.
// Returned value is only valid within the handler. Do not store any references.
// Make copies or use the Immutable setting instead.
func (c *Ctx) Body() []byte {
var err error
var encoding string
var body []byte
// faster than peek
c.Request().Header.VisitAll(func(key, value []byte) {
if utils.UnsafeString(key) == HeaderContentEncoding {
encoding = utils.UnsafeString(value)
}
})
switch encoding {
case StrGzip:
body, err = c.fasthttp.Request.BodyGunzip()
case StrBr, StrBrotli:
body, err = c.fasthttp.Request.BodyUnbrotli()
case StrDeflate:
body, err = c.fasthttp.Request.BodyInflate()
default:
body = c.fasthttp.Request.Body()
}
if err != nil {
return []byte(err.Error())
}
return body
}

Code Snippet (optional)

No response

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
@ReneWerner87
Copy link
Member

just use c.fasthttp.Request.Body() like we do

@alanhe
Copy link
Author

alanhe commented Sep 14, 2022

Thank you very much for the tip! c.Request().Body()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants