how can i solve dio https status 401 error?

1,551

Try to add token to api headers like the following:

  Map<String, String> _headers = <String, String>{
    'Content-Type': 'application/json',
    'Accept': 'application/json',
  };

makeRequest(){
_headers['access_token'] = "YOUR_ACCESS_TOKEN";
    Dio dio = new Dio();
  dio.get(url,
          options: Options(
            headers: _headers,
          )));
}

Notice that the key may not be "access_token" it depends on the backend

Share:
1,551
Hira Kauchha
Author by

Hira Kauchha

Updated on December 31, 2022

Comments

  • Hira Kauchha
    Hira Kauchha over 1 year
    I/flutter (16411): DioError [DioErrorType.response]: Http status error [401]
    I/flutter (16411): #0      DioMixin.assureDioError (package:dio/src/dio_mixin.dart:819:20)
    I/flutter (16411): #1      DioMixin._dispatchRequest (package:dio/src/dio_mixin.dart:678:13)
    I/flutter (16411): <asynchronous suspension>
    I/flutter (16411): #2      DioMixin.fetch.<anonymous closure>.<anonymous closure> (package:dio/src/dio_mixin.dart)
    I/flutter (16411): <asynchronous suspension>
    
    • John Joe
      John Joe over 2 years
      have you tried using any API testing tool ?
    • iwpz
      iwpz over 2 years
      401 means Unauthorized. You need to make sure, how to make this API authorized. Such as add some token to the header or whatever.
    • Hira Kauchha
      Hira Kauchha over 2 years
      I have added token as Dio().options.headers["Authorization"] = "Bearer $token"; but also it reflects same error