resolveRequestEncoding method
Resolves the appropriate character encoding for reading the request body.
This method determines the encoding to use when reading data from an
HttpInputMessage (typically a ServerHttpRequest).
Resolution Logic
- Checks the
Content-Typeheader for acharsetparameter.
Example:Content-Type: application/json; charset=utf-16 - If a charset is found, attempts to resolve it via Encoding.getByName.
- If no valid charset is specified or resolution fails, falls back to getDefaultEncoding (UTF-8 by default).
Parameters
inputMessage– The request message whose encoding should be determined.
Returns
The resolved Encoding to be used for reading the request body.
Example
final encoding = resolveRequestEncoding(request);
final body = await request.getBody().decode(encoding);
Implementation
@protected
Encoding resolveRequestEncoding(HttpInputMessage inputMessage) {
final contentType = inputMessage.getHeaders().getContentType();
final charset = contentType?.getCharset();
return charset != null ? Encoding.getByName(charset) ?? getDefaultEncoding() : getDefaultEncoding();
}