Dart Flutter - http request with body x-www-form-urlencoded

471 Views Asked by At

I want to execute an API Call http post with x-www-form-urlencoded bodies. But, it's never succeed. Backend sends error callback 'field required & value missing'. So, here is my code:

API Calls :

Future<dynamic> apiJsonPostLogin(
String url,
Map<String, dynamic> params,
) async {
Map<String, String> headers = {
  'Content-Type': 'application/x-www-form-urlencoded',
};
log('headers : $headers');
log('api url : $baseUrl$url');

var r = await http.post(Uri.parse(baseUrl + url),
    headers: headers,
    body: jsonEncode(params),
    encoding: Encoding.getByName("utf-8"));
var data = jsonDecode(r.body);
log('status code : ${r.statusCode}');

return data;
}

Specific API Calls With Endpoint :

Future<dynamic> authentication({
required String username,
required String password,
}) async {
final payload = {
  "username": username,
  "password": password,
};

var r = await Api().apiJsonPostLogin('login', payload);
return r;
}

In another case http request with json bodies, i'm never face this kind error with all the lines above (of course with headers content-Type: application/json).

When i tried it in postman, it's going well.

I've already find & try several solutions, but it's never works for my case.

Appreciate for any answers :)

0

There are 0 best solutions below