setDefaultRequestHeaders method
Configures default headers which are usually set by DioMixin
.
- content-type
- content-length
Implementation
Future<void> setDefaultRequestHeaders(Dio dio, RequestOptions options) async {
final data = options.data;
if (data != null &&
RequestMethods.allowedPayloadMethods
.contains(RequestMethods.forName(name: options.method))) {
if (data is FormData) {
options.headers[Headers.contentTypeHeader] =
'multipart/form-data; boundary=${data.boundary}';
options.headers[Headers.contentLengthHeader] = data.length.toString();
} else {
final data = await dio.transformer.transformRequest(options);
List<int> bytes;
if (options.requestEncoder != null) {
bytes = await options.requestEncoder!(data, options);
} else {
bytes = utf8.encode(data);
}
options.headers[Headers.contentLengthHeader] = bytes.length.toString();
}
}
}