fromRequestOptions static method
String
fromRequestOptions(
- RequestOptions options
)
Implementation
static String fromRequestOptions(RequestOptions options) {
final buffer = StringBuffer('curl -X ${options.method}');
final headers = <String, dynamic>{...options.headers};
final contentType = options.contentType;
if (contentType != null &&
!headers.keys.any(
(k) => k.toLowerCase() == Headers.contentTypeHeader,
)) {
headers[Headers.contentTypeHeader] = contentType;
}
// Dart HttpClient defaults — never present on RequestOptions.
// Only added on native (dart:io); browsers set their own.
if (!kIsWeb) {
headers.putIfAbsent(
HttpHeaders.userAgentHeader,
() => 'Dart/${Platform.version.split(' ').first} (dart:io)',
);
headers.putIfAbsent(HttpHeaders.acceptEncodingHeader, () => 'gzip');
headers.putIfAbsent(HttpHeaders.hostHeader, () => _hostHeader(options.uri));
}
headers.forEach((key, value) {
if (value == null) return;
// Dio may store multi-value headers (e.g. Cookie) as a List.
final headerValue = value is Iterable
? value.map((e) => e.toString()).join('; ')
: value.toString();
buffer.write(
" -H '${_escapeShell(key)}: ${_escapeShell(headerValue)}'",
);
});
if (options.data != null) {
if (options.data is FormData) {
_writeFormData(buffer, options.data as FormData);
} else {
final body = _encodeBody(options.data);
buffer.write(" --data '${_escapeShell(body)}'");
}
}
buffer.write(" '${options.uri}'");
return buffer.toString();
}