Skip to content

Commit 67fcc66

Browse files
committed
Update HAR codegen
1 parent e4f0633 commit 67fcc66

File tree

2 files changed

+321
-46
lines changed

2 files changed

+321
-46
lines changed

lib/utils/har_utils.dart

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
// http://www.softwareishard.com/blog/har-12-spec/
2+
// https://github.com/ahmadnassri/har-spec/blob/master/versions/1.2.md
3+
14
import 'dart:convert';
25
import 'package:apidash/consts.dart';
3-
import 'package:apidash/utils/utils.dart' show getValidRequestUri;
6+
import 'package:apidash/utils/utils.dart'
7+
show getValidRequestUri, getNewUuid, getFilenameFromPath;
48
import 'package:apidash/models/models.dart' show RequestModel;
59
import 'package:package_info_plus/package_info_plus.dart';
610

@@ -75,6 +79,7 @@ Map<String, dynamic> requestModelToHARJsonRequest(
7579
defaultUriScheme = kDefaultUriScheme,
7680
bool exportMode = false,
7781
bool useEnabled = false,
82+
String? boundary,
7883
}) {
7984
Map<String, dynamic> json = {};
8085
bool hasBody = false;
@@ -110,21 +115,37 @@ Map<String, dynamic> requestModelToHARJsonRequest(
110115
}
111116
}
112117

113-
var method = requestModel.method;
114-
var requestBody = requestModel.requestBody;
115-
if (kMethodsWithBody.contains(method) &&
116-
requestBody != null &&
117-
!requestModel.isFormDataRequest) {
118-
var contentLength = utf8.encode(requestBody).length;
119-
if (contentLength > 0) {
120-
hasBody = true;
121-
json["postData"] = {};
122-
json["postData"]["mimeType"] =
123-
requestModel.requestBodyContentType.header;
124-
json["postData"]["text"] = requestBody;
125-
if (exportMode) {
126-
json["postData"]["comment"] = "";
118+
if (requestModel.hasJsonData || requestModel.hasTextData) {
119+
hasBody = true;
120+
json["postData"] = {};
121+
json["postData"]["mimeType"] = requestModel.requestBodyContentType.header;
122+
json["postData"]["text"] = requestModel.requestBody;
123+
if (exportMode) {
124+
json["postData"]["comment"] = "";
125+
}
126+
}
127+
128+
if (requestModel.hasFormData) {
129+
boundary = boundary ?? getNewUuid();
130+
hasBody = true;
131+
json["postData"] = {};
132+
json["postData"]["mimeType"] =
133+
"${requestModel.requestBodyContentType.header}; boundary=$boundary";
134+
json["postData"]["params"] = [];
135+
for (var item in requestModel.formDataList) {
136+
Map<String, String> d = exportMode ? {"comment": ""} : {};
137+
if (item.type == FormDataType.text) {
138+
d["name"] = item.name;
139+
d["value"] = item.value;
140+
}
141+
if (item.type == FormDataType.file) {
142+
d["name"] = item.name;
143+
d["fileName"] = getFilenameFromPath(item.value);
127144
}
145+
json["postData"]["params"].add(d);
146+
}
147+
if (exportMode) {
148+
json["postData"]["comment"] = "";
128149
}
129150
}
130151

@@ -137,8 +158,8 @@ Map<String, dynamic> requestModelToHARJsonRequest(
137158
if (headers.isNotEmpty || hasBody) {
138159
if (hasBody && !requestModel.hasContentTypeHeader) {
139160
var m = {
140-
"name": "Content-Type",
141-
"value": requestModel.requestBodyContentType.header
161+
"name": kHeaderContentType,
162+
"value": json["postData"]["mimeType"]
142163
};
143164
if (exportMode) {
144165
m["comment"] = "";
@@ -154,14 +175,12 @@ Map<String, dynamic> requestModelToHARJsonRequest(
154175
}
155176
}
156177
}
157-
if (requestModel.isFormDataRequest) {
158-
json["formData"] = requestModel.formDataMapList;
159-
}
160178
if (exportMode) {
161179
json["comment"] = "";
162180
json["cookies"] = [];
163181
json["headersSize"] = -1;
164-
json["bodySize"] = hasBody ? utf8.encode(requestBody!).length : 0;
182+
json["bodySize"] =
183+
hasBody ? utf8.encode(json["postData"]["text"] ?? "").length : 0;
165184
}
166185
}
167186
return json;

0 commit comments

Comments
 (0)