postRequest function
POST request to fetch data from URL
Implementation
Future<String?> postRequest(
Uri url, {
Object? body,
Map<String, String> headers = const {},
bool debug = false,
ProxyAPIConfig? proxyAPIConfig,
}) async {
printLog("HTTP POST URL: $url", debug, color: LogColor.magenta);
if (proxyAPIConfig != null) {
printLog("Prepending proxy URL...", debug, color: LogColor.yellow);
url = proxyAPIConfig.generateUrl(url);
}
printLog("HTTP Parser Headers: $headers", debug, color: LogColor.magenta);
printLog("HTTP Parser Payload: $body", debug, color: LogColor.magenta);
try {
var html = await http
.post(
url,
body: body,
headers: headers,
)
.timeout(
Duration(seconds: 30),
);
printLog(
"HTTP Response: ${html.statusCode}",
debug,
color: LogColor.magenta,
);
throwIfNotFound(html.statusCode, url, debug);
String responseBody = utf8.decode(html.bodyBytes);
dumpResponseToFile(html: responseBody, debug: debug);
return responseBody;
} on WebScraperError {
rethrow;
} catch (e) {
printLog(e.toString(), debug, color: LogColor.red);
return null;
}
}