Change content-type in Dio request
2,629
Thanks to Anis advice it worked! Indeed the problem was in FormData
Map<String, String> loginBody4 = {
'code': code,
'grant_type': 'authorization_code',
'redirect_uri': 'courier-mobile-app://auth/login',
'code_verifier': 'application_application_application_application_application',
'client_id': 'courier_mobile_app',
'client_secret': 'secret'
};
response = await dio.post(urlString4, data: loginBody4, options: Options(contentType: Headers.formUrlEncodedContentType));
this also works:
dio.options.headers['content-Type'] = 'application/x-www-form-urlencoded';
response = await dio.post(urlString4, data: loginBody4);
Author by
Kosh
Updated on January 04, 2023Comments
-
Kosh over 1 year
Please tell me how can I change content-type of my request with Dio? I am trying to change it by adding
contentType: Headers.formUrlEncodedContentType
into options but it looks like to no avail.My code is:
final String urlString4 = protocol + baseUrl + '/connect/token'; var loginBody4 = FormData.fromMap({ 'code': code, 'grant_type': 'authorization_code', 'redirect_uri': 'courier-mobile-app://auth/login', 'code_verifier': 'application_application_application_application_application', 'client_id': 'courier_mobile_app', 'client_secret': 'secret' }); try { response = await dio.post(urlString4, data: loginBody4, options: Options( contentType: Headers.formUrlEncodedContentType ) ); } on DioError catch (e) { print('Request failed with status: ${e.response?.data}'); }
but my intercepted request looks like this:
curl -H "user-agent: Dart/2.16 (dart:io)" -H "content-type: multipart/form-data; boundary=--dio-boundary-3038081899" -H "cookie: .AspNetCore.Antiforgery.2P6h…..h3g" -H "host: auth.dev-drive.XXX.dev --data-binary "----dio-boundary-3038081899 content-disposition: form-data; name=\"code\" C8C3E5D1ACE01DF61F988E06C391AAB3347BD2A64A0B6F56E5DC225710BEF672 ----dio-boundary-3038081899 content-disposition: form-data; name=\"grant_type\" authorization_code ----dio-boundary-3038081899 content-disposition: form-data; name=\"redirect_uri\" courier-mobile-app://auth/login ----dio-boundary-3038081899 content-disposition: form-data; name=\"code_verifier\" application_application_application_application_application ----dio-boundary-3038081899 content-disposition: form-data; name=\"client_id\" courier_mobile_app ----dio-boundary-3038081899 content-disposition: form-data; name=\"client_secret\" secret ----dio-boundary-3038081899-- " --compressed "https://auth.dev-drive.XXX.dev/connect/token"
I tried this way
dio.options.headers['content-Type'] = 'application/x-www-form-urlencoded'; response = await dio.post(urlString4, data: loginBody4);
but result is the same:
content-type: multipart/form-data; boundary=--dio-boundary-3428087265
-
Anis Alibegić about 2 yearsI believe it has something to do with the
FormData
. Try sending a request without body to see ifcontent-type
header is changed.
-