【问题标题】:api response body doesn't show upapi响应正文不显示
【发布时间】:2022-02-01 04:22:06
【问题描述】:

所以我正在尝试从 Instagram API 获取访问令牌。它显示在 Postman 中,但未显示在响应正文中

    onPageStarted: (url) async {
      print("url is " + url);

      if (url
          .startsWith("https://aymen-ziouche.github.io/Gaming-website/")) {
        Navigator.pop(context);
        var uri = Uri.parse(url);
        final String? code = uri.queryParameters["code"];
        print("this's the code: $code");
        final response = await dio
            .post('https://api.instagram.com/oauth/access_token', data: {
          'client_id': Constants.igClientId,
          'client_secret': Constants.igClientSecret,
          'grant_type': "authorization_code",
          'redirect_uri': Constants.igRedirectURL,
          'code': code
        });

        print("token is => ${response.data}");
        print("response => ${response.statusCode} ${response.data}");
      }
    },

这是日志:

I/flutter (21891): url is https://aymen-ziouche.github.io/Gaming-website/?code=AQDIXj5sEEPfgHv_U5dcw3A9zQbWjR9w37MTWg9f153UAJsyHcG34iNtSgxH9sswpBvHrB1CobZBgReCx4GhCIeq5HiLfIDRe8c_HNS_U_7CQ5fVdP6JdS1vLxQua4iA706q9lik08r_C9aAFLm6oT0hJeQVXcHiwTqJWLxxyXxWhtmeClBYwlIoSJKp8gcf-rft6QrVOolBcXg6LCB6Vj9W7Dr3k4QiqsDVN0vX2uvAsw#_
I/flutter (21891): this's the code: AQDIXj5sEEPfgHv_U5dcw3A9zQbWjR9w37MTWg9f153UAJsyHcG34iNtSgxH9sswpBvHrB1CobZBgReCx4GhCIeq5HiLfIDRe8c_HNS_U_7CQ5fVdP6JdS1vLxQua4iA706q9lik08r_C9aAFLm6oT0hJeQVXcHiwTqJWLxxyXxWhtmeClBYwlIoSJKp8gcf-rft6QrVOolBcXg6LCB6Vj9W7Dr3k4QiqsDVN0vX2uvAsw
I/flutter (21891): token is =>
I/flutter (21891): response => 200

我修好了。问题出在 dio.options() 我添加了这个:Options(responseType: ResponseType.json)

【问题讨论】:

  • 如果您提供日志的屏幕截图会很有帮助......就像 API 正在准确回复一样
  • 是的,对不起。在这里

标签: flutter api dart


【解决方案1】:

将“Content-Type”:“application/json”和“Accept”:“application/json”添加到 dio.post() 方法的 headers 参数中

【讨论】:

    【解决方案2】:

    根据the package example,如果要打印,请使用response.data.toString()

    如果你想使用它,请先正确decode它。

    【讨论】:

    • 试过了。不工作
    猜你喜欢
    • 1970-01-01
    • 2017-12-15
    • 2020-08-12
    • 1970-01-01
    • 2016-11-04
    • 2014-04-13
    • 1970-01-01
    • 2018-04-02
    • 2015-07-05
    相关资源
    最近更新 更多