【问题标题】:Why I do not get response using Dio in flutter?Why I do not get response using Dio in flutter?
【发布时间】:2022-12-27 05:40:26
【问题描述】:

I am new to dio and I tried to create login function, here is my dio.post code

Future<LoginModel> loginUser(LoginPost data, String method) async {
try {
  final response = await _dio.post(
    'api/users/v1/login/$method',
    data: json.encode(data.toJson(method)),
    options: Options(
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic ${base64Encode(
          utf8.encode('$clientId:$clientSecret'),
        )}',
      },
    ),
  );
  LoginModel model = LoginModel.fromJson(json.decode(response.data));
  print(model.success);
  return model;
} catch (error, stacktrace) {
  _printError(error, stacktrace);
  return LoginModel.withError(false, '', '$error', 0);
}
}

and the program only run onRequest Function, the onResponse function is not run by the program. The url, auth, body, etc. are already correct, and I tried using Postman, and the endpoint works. Does anyone know why the program does not receive a response?

【问题讨论】:

  • Not 1005 sure, but you are sending a Content-Type as form urlencoded but the body data as JSON. You can try changing the Content-Type to application/json and see if that works.
  • its still not work
  • Update the question showing a working Postman request.
  • @RichardHeap already
  • Very basic question, but, could you reach the API from the device's browser? It's a mobile app? If it's a desktop app on osx, does it has internet permission?

标签: flutter dio


【解决方案1】:

To send data in the application/json format (default for dio)

dio.post(
  '/info', 
  data: {'id': 5},
);

To send data in the application/x-www-form-urlencoded format

dio.post(
  '/info',
  data: {'id': 5},
  options: Options(contentType: Headers.formUrlEncodedContentType),
);

【讨论】:

  • still no response receive
  • What is response.statusCode and response.data? Print out right after post call.
  • I already tried to print it, but it doesn't display
  • @ParamaArtha you got ans because i have facing same issue
【解决方案2】:

This code certainly gets a response - and works in dartpad, so try it there. (Of course the response is 401, as I don't have the credentials.)

import 'dart:convert';

import 'package:http/http.dart' as http;

void main() async {
  final auth = base64.encode(utf8.encode('cid:secret'));
  final r = await http.post(
    Uri.https('api.komplekku.com', 'core/api/users/v1/login/email'),
    headers: {
      'authorization': 'Basic $auth',
    },
    body: {
      'email': 'foo',
      'password': 'bar',
    },
  );

  print(r.statusCode);
  print(r.body);
}

【讨论】:

    【解决方案3】:

    If you are using a custom interceptor and using v4+ of dio, remember to call handler.next(..); or .resolve(..) or .reject(..) depending on the method you are overwriting and how you are proceeding in your specific case.

    i.e. for intercepting onRequest it would look as this:

    dio.interceptors.add(InterceptorsWrapper(onRequest: (options, handler) {
      if (token != null) {
        options.headers['Authorization'] = 'Bearer ${token!}';
      }
      return handler.next(options);
    }));
    

    【讨论】:

      猜你喜欢
      • 2022-12-27
      • 2022-12-27
      • 2022-12-01
      • 2023-02-26
      • 2022-12-27
      • 2022-12-27
      • 2021-02-26
      • 2022-12-02
      • 2022-12-02
      相关资源
      最近更新 更多