Dio: DioError [DioErrorType.RESPONSE]: Http status error [401]

Created on 14 Mar 2019  ·  2Comments  ·  Source: flutterchina/dio

使用Basic Authentication方式请求github的https://api.github.com/authorizations

final requestHeader = {'Authorization': 'Basic '+base64.encode(utf8.encode('$userName:$passWord'))};
    final requestBody = json.encode({
      'client_id': 'xxxxx',
      'client_secret': 'xxxxxxxxx',
      'scopes': ['user', 'repo', 'notifications']
    });
    try {
      Response response;
      Dio dio = new Dio();
      response = await dio.post(
        Api.authorizations,
        data: requestBody,
        options: Options(
          headers: requestHeader,
        ),
      );
      return response;
    } catch (e) {
      print(e);
      return null;
    }

同样的参数,我使用postman进行请求返回成功,但是这里永远是401

Most helpful comment

I solved this way:

var response = await Dio().post("http://myurl",
    options: Options(
        followRedirects: false,
        validateStatus: (status) { return status < 500; }
    ),
);

All 2 comments

@dlgchg 请问是什么原因引起的?

I solved this way:

var response = await Dio().post("http://myurl",
    options: Options(
        followRedirects: false,
        validateStatus: (status) { return status < 500; }
    ),
);
Was this page helpful?
0 / 5 - 0 ratings