curl property
String?
get
curl
Generates a cURL command based on the request details of the Dio exception.
Returns:
- A
String
containing the cURL command if successful, ornull
if an error occurs.
Usage Example:
try {
await VenturoApiManager.api.get("https://example.com");
} on DioException catch (e) {
print(e.curl);
}
Implementation
String? get curl {
try {
final qp = requestOptions.queryParameters;
final Map<String, dynamic> h = Map.from(requestOptions.headers);
h.addAll({
"user-agent": "Dart/3.3 (dart:io)",
"accept-encoding": "gzip",
"host": requestOptions.uri.host,
});
final d = requestOptions.data;
final StringBuffer curlBuffer = StringBuffer();
// Start building the cURL command
curlBuffer.write(
"curl --location --request ${requestOptions.method} '${requestOptions.baseUrl}${requestOptions.path}");
// Append query parameters if present
if (qp.isNotEmpty) {
curlBuffer.write(qp.keys.fold(
'',
(value, key) => '$value${value.toString().isEmpty ? '?' : '&'}$key=${qp[key]}',
));
}
curlBuffer.write("'");
// Append headers
h.forEach((key, value) {
curlBuffer.write(" \\\n--header '$key: $value'");
});
// Handling FormData
if (d is FormData) {
for (var field in d.fields) {
curlBuffer.write(" \\\n--form '${field.key}=${field.value}'");
}
for (var file in d.files) {
curlBuffer.write(" \\\n--form '${file.key}=@${file.value.filename}'");
}
}
// Handling JSON/Raw Body
else if (d != null && d.toString().isNotEmpty) {
curlBuffer.write(" \\\n--data-raw '${jsonEncode(d)}'");
}
final curlCommand = curlBuffer.toString();
debugPrint("$curlCommand - [CURL]");
return curlCommand;
} catch (e, s) {
debugPrint("$e");
debugPrint("$s");
}
return null;
}