transformRequest method

  1. @override
Future<String> transformRequest(
  1. RequestOptions options
)
override

transformRequest allows changes to the request data before it is sent to the server, but after the RequestInterceptor.

This is only applicable for request methods 'PUT', 'POST', and 'PATCH'

Implementation

@override
Future<String> transformRequest(RequestOptions options) async {
  final Object data = options.data ?? '';
  if (data is! String && Transformer.isJsonMimeType(options.contentType)) {
    return jsonEncodeCallback(data);
  } else if (data is Map) {
    if (data is Map<String, dynamic>) {
      return Transformer.urlEncodeMap(data, options.listFormat);
    }
    debugLog(
      'The data is a type of `Map` (${data.runtimeType}), '
      'but the transformer can only encode `Map<String, dynamic>`.\n'
      'If you are writing maps using `{}`, '
      'consider writing `<String, dynamic>{}`.',
      StackTrace.current,
    );
    return data.toString();
  } else {
    return data.toString();
  }
}