2

Upon sending login data to a remote URL, the website returns a 302 along with the proper login Cookies. When processing it with http or HttpClient, the status code 302 is processed as good, however with Dio it's considered as an error.

How to make Dio handle the HTTP 302 code as a regular HTTP code and not an error ?

Code

Dio dio = new Dio();
dio.options.contentType = ContentType('application','x-www-form-urlencoded');
dio.post(urlLogin, data: data).then((Response resp){
  print('-----Login-----');
  List<String> headers = resp.headers['set-cookie'];
  headers.forEach((h) => print(h));
});

Stack Trace

Unhandled exception:
DioError [DioErrorType.RESPONSE]: Http status error [302]
#0      _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:1112:29)
#1      _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#2      _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#3      _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:391:30)
#4      _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
#5      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)
Elcan
  • 814
  • 13
  • 27
  • My bad, did some bad research, voted to close it in favor of https://stackoverflow.com/questions/52537500/flutter-handle-status-code-302-in-post-request?rq=1 – Elcan Oct 04 '18 at 13:16
  • 0 i got a similar problem and i solved it with adding header with "Accept":"application/json" . henceforth it will only return json data otherwise it will prompt to redirect with html url. – A.K.J.94 Mar 26 '21 at 11:05

0 Answers0