1
+ // http://www.softwareishard.com/blog/har-12-spec/
2
+ // https://github.com/ahmadnassri/har-spec/blob/master/versions/1.2.md
3
+
1
4
import 'dart:convert' ;
2
5
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;
4
8
import 'package:apidash/models/models.dart' show RequestModel;
5
9
import 'package:package_info_plus/package_info_plus.dart' ;
6
10
@@ -75,6 +79,7 @@ Map<String, dynamic> requestModelToHARJsonRequest(
75
79
defaultUriScheme = kDefaultUriScheme,
76
80
bool exportMode = false ,
77
81
bool useEnabled = false ,
82
+ String ? boundary,
78
83
}) {
79
84
Map <String , dynamic > json = {};
80
85
bool hasBody = false ;
@@ -110,21 +115,37 @@ Map<String, dynamic> requestModelToHARJsonRequest(
110
115
}
111
116
}
112
117
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);
127
144
}
145
+ json["postData" ]["params" ].add (d);
146
+ }
147
+ if (exportMode) {
148
+ json["postData" ]["comment" ] = "" ;
128
149
}
129
150
}
130
151
@@ -137,8 +158,8 @@ Map<String, dynamic> requestModelToHARJsonRequest(
137
158
if (headers.isNotEmpty || hasBody) {
138
159
if (hasBody && ! requestModel.hasContentTypeHeader) {
139
160
var m = {
140
- "name" : "Content-Type" ,
141
- "value" : requestModel.requestBodyContentType.header
161
+ "name" : kHeaderContentType ,
162
+ "value" : json[ "postData" ][ "mimeType" ]
142
163
};
143
164
if (exportMode) {
144
165
m["comment" ] = "" ;
@@ -154,14 +175,12 @@ Map<String, dynamic> requestModelToHARJsonRequest(
154
175
}
155
176
}
156
177
}
157
- if (requestModel.isFormDataRequest) {
158
- json["formData" ] = requestModel.formDataMapList;
159
- }
160
178
if (exportMode) {
161
179
json["comment" ] = "" ;
162
180
json["cookies" ] = [];
163
181
json["headersSize" ] = - 1 ;
164
- json["bodySize" ] = hasBody ? utf8.encode (requestBody! ).length : 0 ;
182
+ json["bodySize" ] =
183
+ hasBody ? utf8.encode (json["postData" ]["text" ] ?? "" ).length : 0 ;
165
184
}
166
185
}
167
186
return json;
0 commit comments