I'm currently stuck on creating an api request with Flutter.
To explain the context, I need to recreate this request:
curl "url" --data "grant_type=password&client_id=x509" -E cert.pem
And to be more precise, the Content-Type of the request header must be of type x-www-form-encoded. Also, I am aware that using a client-side certificate is not secure, but this is an internal application.
My code :
var cert = http.MultipartFile.fromBytes('x509',
(await rootBundle.load('assets/keys/login.pem')).buffer.asInt8List(),
filename: 'login.pem');
Map<String, dynamic> headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/x-www-form-urlencoded'
};
var request = http.MultipartRequest(
"POST",
Uri.parse(
url));
headers['content-type'] = headers;
request.fields['grant_type'] = 'password';
request.fields['client_id'] = 'x509';
request.files.add(cert);
// Wait response and decode JSON.
var response = await request.send();
var responseData = await response.stream.toBytes();
var result = jsonDecode(utf8.decode(responseData.toList()));
My problem :
Response code : 415
{"error":"RESTEASY003065: Cannot consume content type"}
With the screenshoot bellow, you'll see that the content-type is auto-set as a multipart/form-data. So I am looking for a way to overwrite it or a method to send my request please.
I guess overwriting it is bad because the api server will not handle the request correctly.
Informations on the request :
EDIT : Now, I know that I can use context, sadly it's not available on Flutter web, do you have any idea ?