added request header support for code generation

This commit is contained in:
Aditya Mayukh Som
2024-03-21 22:17:52 +05:30
parent 6e86422285
commit 0734cc2862

View File

@ -74,6 +74,7 @@ public class Main {
try {
String result = '';
bool hasBody = false;
var rec = getValidRequestUri(
requestModel.url,
requestModel.enabledRequestParams,
@ -140,6 +141,52 @@ public class Main {
result +=
templateRequestCreation.render({"method": method.name.toLowerCase()});
// ~~~~~~~~~~~~~~~~~~ request header start ~~~~~~~~~~~~~~~~~~
var m = <String, String>{};
for (var i in harJson["headers"]) {
m[i["name"]] = i["value"];
}
// especially sets up Content-Type header if the request has a body
// and Content-Type is not explicitely set by the developer
if (requestModel.hasBody &&
requestModel.hasFormData &&
!requestModel.hasFileInFormData) {
m["Content-Type"] = "application/x-www-form-urlencoded";
}
// we will use this request boundary to set boundary if multipart formdata is used
String requestBoundary = "";
String multipartTypePrefix = "multipart/form-data; boundary=";
if (m.containsKey("Content-Type") &&
m["Content-Type"] != null &&
m["Content-Type"]!.startsWith(RegExp(multipartTypePrefix))) {
String tmp = m["Content-Type"]!;
requestBoundary = tmp.substring(multipartTypePrefix.length);
// if a boundary is provided, we will use that as the default boundary
if (boundary != null) {
requestBoundary = boundary;
m["Content-Type"] = multipartTypePrefix + boundary;
}
}
var templateRequestHeader = jj.Template(kTemplateRequestHeader);
// setting up rest of the request headers
m.forEach((name, value) {
result += templateRequestHeader.render({"name": name, "value": value});
});
// if (hasBody && !m.containsKey("Content-Type")) {
// var templateContentType = jj.Template(kTemplateContentType);
// result += templateContentType.render(
// {"contentType": requestModel.requestBodyContentType.header});
// }
// ~~~~~~~~~~~~~~~~~~ request header ends ~~~~~~~~~~~~~~~~~~
return result;
} catch (e) {