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
Author by
Hira Kauchha
Updated on December 31, 2022Comments
-
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 over 2 yearshave you tried using any API testing tool ?
-
iwpz over 2 years401 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 over 2 yearsI have added token as Dio().options.headers["Authorization"] = "Bearer $token"; but also it reflects same error
-