I\'m trying to make an post request in flutter with content type as url encoded. When I write body : json.encode(data)
, it encodes to plain text.
If I
I'd like to recommend dio package to you , dio is a powerful Http client for Dart/Flutter, which supports Interceptors, FormData, Request Cancellation, File Downloading, Timeout etc.
dio is very easy to use, in your case you can:
Map<String, String> body = {
'name': 'doodle',
'color': 'blue',
'teamJson': {
'homeTeam': {'team': 'Team A'},
'awayTeam': {'team': 'Team B'},
},
};
dio.post("/info",data:body, options:
new Options(contentType:ContentType.parse("application/x-www-form-urlencoded")))
dio can encode the data automatically.
More details please refer to dio.
I did it like this with dart's http package. It's not too fancy. My endpoint wasn't accepting the parameters with other methods, but it accepted them like this, with the brackets included in the parameter.
import 'package:http/http.dart' as http;
String url = "<URL>";
Map<String, String> match = {
"homeTeam[team]": "Team A",
"awayTeam[team]": "Team B",
};
Map<String, String> headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
http.post(url, body: body, headers: headers).then((response){
print(response.toString());
});