Committed by
GitHub
Merge pull request #1288 from aramayyes/patch-1
Construct request with body for form-urlencoded.
Showing
1 changed file
with
8 additions
and
6 deletions
| @@ -103,18 +103,20 @@ class GetHttpClient { | @@ -103,18 +103,20 @@ class GetHttpClient { | ||
| 103 | headers['content-length'] = bodyBytes.length.toString(); | 103 | headers['content-length'] = bodyBytes.length.toString(); | 
| 104 | headers['content-type'] = | 104 | headers['content-type'] = | 
| 105 | 'multipart/form-data; boundary=${body.boundary}'; | 105 | 'multipart/form-data; boundary=${body.boundary}'; | 
| 106 | + } else if (contentType != null && contentType.toLowerCase() == 'application/x-www-form-urlencoded' && body is Map) { | ||
| 107 | + var parts = []; | ||
| 108 | + (body as Map<String, dynamic>).forEach((key, value) { | ||
| 109 | + parts.add('${Uri.encodeQueryComponent(key)}=' | ||
| 110 | + '${Uri.encodeQueryComponent(value.toString())}'); | ||
| 111 | + }); | ||
| 112 | + var formData = parts.join('&'); | ||
| 113 | + bodyBytes = utf8.encode(formData); | ||
| 106 | } else if (body is Map || body is List) { | 114 | } else if (body is Map || body is List) { | 
| 107 | var jsonString = json.encode(body); | 115 | var jsonString = json.encode(body); | 
| 108 | 116 | ||
| 109 | bodyBytes = utf8.encode(jsonString); | 117 | bodyBytes = utf8.encode(jsonString); | 
| 110 | headers['content-length'] = bodyBytes.length.toString(); | 118 | headers['content-length'] = bodyBytes.length.toString(); | 
| 111 | headers['content-type'] = contentType ?? defaultContentType; | 119 | headers['content-type'] = contentType ?? defaultContentType; | 
| 112 | - //TODO check this implementation | ||
| 113 | - if (contentType != null) { | ||
| 114 | - if (contentType.toLowerCase() == 'application/x-www-form-urlencoded') { | ||
| 115 | - jsonString = Uri.encodeQueryComponent(jsonString); | ||
| 116 | - } | ||
| 117 | - } | ||
| 118 | } else if (body is String) { | 120 | } else if (body is String) { | 
| 119 | bodyBytes = utf8.encode(body); | 121 | bodyBytes = utf8.encode(body); | 
| 120 | headers['content-length'] = bodyBytes.length.toString(); | 122 | headers['content-length'] = bodyBytes.length.toString(); | 
- 
Please register or login to post a comment