urlencoded request in flutter code example
Example 1: dart how to convert json to x-www-form-urlencoded
Future<HttpClientResponse> foo() async {
Map<String, dynamic> jsonMap = {
'homeTeam': {'team': 'Team A'},
'awayTeam': {'team': 'Team B'},
};
String jsonString = json.encode(jsonMap);
String paramName = 'param';
String formBody = paramName + '=' + Uri.encodeQueryComponent(jsonString);
List<int> bodyBytes = utf8.encode(formBody);
HttpClientRequest request =
await _httpClient.post(_host, _port, '/a/b/c');
request.headers.set('Content-Length', bodyBytes.length.toString());
request.add(bodyBytes);
return await request.close();
}
Example 2: dart how to convert json to x-www-form-urlencoded
Map<String, String> body = {
'name': 'doodle',
'color': 'blue',
'homeTeam': json.encode(
{'team': 'Team A'},
),
'awayTeam': json.encode(
{'team': 'Team B'},
),
};
Response r = await post(
url,
body: body,
);