change method

  1. @override
Request change({
  1. Map<String, Object?>? headers,
  2. Map<String, Object?>? context,
  3. String? path,
  4. Object? body,
})

Creates a new Request by copying existing values and applying specified changes.

New key-value pairs in context and headers will be added to the copied Request. If context or headers includes a key that already exists, the key-value pair will replace the corresponding entry in the copied Request. If context or headers contains a null value the corresponding key will be removed if it exists, otherwise the null value will be ignored. For headers a value which is an empty list will also cause the corresponding key to be removed.

All other context and header values from the Request will be included in the copied Request unchanged.

body is the request body. It may be either a String, a List<int>, a Stream<List<int>>, or null to indicate no body.

path is used to update both handlerPath and url. It's designed for routing middleware, and represents the path from the current handler to the next handler. It must be a prefix of url; handlerPath becomes handlerPath + "/" + path, and url becomes relative to that. For example:

print(request.handlerPath); // => /static/
print(request.url);        // => dir/file.html

request = request.change(path: "dir");
print(request.handlerPath); // => /static/dir/
print(request.url);        // => file.html

Implementation

@override
Request change({
  Map<String, /* String | List<String> */ Object?>? headers,
  Map<String, Object?>? context,
  String? path,
  Object? body,
}) {
  final headersAll = updateHeaders(this.headersAll, headers);
  final newContext = updateMap<String, Object>(this.context, context);

  body ??= extractBody(this);

  var handlerPath = this.handlerPath;
  if (path != null) handlerPath += path;

  return Request._(method, requestedUri,
      protocolVersion: protocolVersion,
      headers: headersAll,
      handlerPath: handlerPath,
      body: body,
      context: newContext,
      onHijack: _onHijack);
}