updated header and query support

This commit is contained in:
Aditya Mayukh Som
2024-03-24 18:53:29 +05:30
parent 8447962bc4
commit b3e62d6b32

View File

@ -173,33 +173,51 @@ public class Main {
templateRequestCreation.render({"method": method.name.toUpperCase()}); templateRequestCreation.render({"method": method.name.toUpperCase()});
// setting up query parameters // setting up query parameters
if (uri.hasQuery) { var params = uri.queryParameters;
var params = uri.queryParameters; if (params.isNotEmpty) {
var templateUrlQueryParam = jj.Template(kTemplateUrlQueryParam); var templateUrlQueryParam = jj.Template(kTemplateUrlQueryParam);
params.forEach((name, value) { result += templateUrlQueryParam.render({"queryParams": params});
result +=
templateUrlQueryParam.render({"name": name, "value": value});
});
} }
result = kTemplateStart + result; var headers = <String, String>{};
for (var i in harJson["headers"]) {
var contentType = requestModel.requestBodyContentType.header; headers[i["name"]] = i["value"];
var templateRequestHeader = jj.Template(kTemplateRequestHeader); }
// especially sets up Content-Type header if the request has a body // especially sets up Content-Type header if the request has a body
// and Content-Type is not explicitely set by the developer // and Content-Type is not explicitely set by the developer
if (hasBody && if (requestModel.hasBody && !headers.containsKey("Content-Type")) {
!requestModel.enabledHeadersMap.containsKey('Content-Type')) { headers["Content-Type"] = requestModel.requestBodyContentType.header;
result += templateRequestHeader }
.render({"name": 'Content-Type', "value": contentType});
if (requestModel.hasBody &&
requestModel.hasFormData &&
!requestModel.hasFileInFormData) {
headers["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 (headers.containsKey("Content-Type") &&
headers["Content-Type"]!.startsWith(RegExp(multipartTypePrefix))) {
// String tmp = headers["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;
headers["Content-Type"] = multipartTypePrefix + boundary;
}
} }
// setting up rest of the request headers // setting up rest of the request headers
var headers = requestModel.enabledHeadersMap; if (headers.isNotEmpty) {
headers.forEach((name, value) { var templateRequestHeader = jj.Template(kTemplateRequestHeader);
result += templateRequestHeader.render({"name": name, "value": value}); result += templateRequestHeader.render({
}); "headers": headers //
});
}
// handling form data // handling form data
if (requestModel.hasFormData && if (requestModel.hasFormData &&