resolveRequestEncoding method

  1. @protected
Encoding resolveRequestEncoding(
  1. HttpInputMessage inputMessage
)

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

  1. Checks the Content-Type header for a charset parameter.
    Example: Content-Type: application/json; charset=utf-16
  2. If a charset is found, attempts to resolve it via Encoding.getByName.
  3. 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();
}