MCPcopy
hub / github.com/gofiber/fiber / Body

Method Body

req.go:150–200  ·  view source on GitHub ↗

Body contains the raw body submitted in a POST request. This method will decompress the body if the 'Content-Encoding' header is provided. It returns the original (or decompressed) body data which is valid only within the handler. Don't store direct references to the returned data. If you need to ke

()

Source from the content-addressed store, hash-verified

148// Don't store direct references to the returned data.
149// If you need to keep the body's data later, make a copy or use the Immutable option.
150func (r *DefaultReq) Body() []byte {
151 var (
152 err error
153 body, originalBody []byte
154 headerEncoding string
155 encodingOrder = []string{"", "", ""}
156 )
157
158 request := &r.c.fasthttp.Request
159
160 // Get Content-Encoding header
161 headerEncoding = utils.UnsafeString(utilsbytes.UnsafeToLower(request.Header.ContentEncoding()))
162
163 // If no encoding is provided, return the original body
164 if headerEncoding == "" {
165 return r.getBody()
166 }
167
168 // Split and get the encodings list, in order to attend the
169 // rule defined at: https://www.rfc-editor.org/rfc/rfc9110#section-8.4-5
170 encodingOrder = getSplicedStrList(headerEncoding, encodingOrder)
171 for i := range encodingOrder {
172 encodingOrder[i] = utilsstrings.UnsafeToLower(encodingOrder[i])
173 }
174 if len(encodingOrder) == 0 {
175 return r.getBody()
176 }
177
178 var decodesRealized uint8
179 body, decodesRealized, err = r.tryDecodeBodyInOrder(&originalBody, encodingOrder)
180
181 // Ensure that the body will be the original
182 if originalBody != nil && decodesRealized > 0 {
183 request.SetBodyRaw(originalBody)
184 }
185 if err != nil {
186 switch {
187 case errors.Is(err, ErrUnsupportedMediaType):
188 _ = r.c.DefaultRes.SendStatus(StatusUnsupportedMediaType) //nolint:errcheck,staticcheck // It is fine to ignore the error and the static check
189 case errors.Is(err, ErrNotImplemented):
190 _ = r.c.DefaultRes.SendStatus(StatusNotImplemented) //nolint:errcheck,staticcheck // It is fine to ignore the error and the static check
191 case errors.Is(err, fasthttp.ErrBodyTooLarge):
192 _ = r.c.DefaultRes.SendStatus(StatusRequestEntityTooLarge) //nolint:errcheck,staticcheck // It is fine to ignore the error and the static check
193 default:
194 // do nothing
195 }
196 return []byte(err.Error())
197 }
198
199 return r.c.app.GetBytes(body)
200}
201
202// RequestCtx returns *fasthttp.RequestCtx that carries a deadline
203// a cancellation signal, and other values across API boundaries.

Callers

nothing calls this directly

Calls 7

getBodyMethod · 0.95
tryDecodeBodyInOrderMethod · 0.95
getSplicedStrListFunction · 0.85
GetBytesMethod · 0.80
IsMethod · 0.65
SendStatusMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected