Aymen Ziouche
Aymen Ziouche

Reputation: 187

api response body doesn't show up

So I'm trying to get the access token from the Instagram API. it shows in Postman but it doesn't show up in the response body

    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}");
      }
    },

here's the log:

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

I fixed it. the problem was in dio.options() I added this: Options(responseType: ResponseType.json)

Upvotes: 0

Views: 660

Answers (2)

M Imam Pratama
M Imam Pratama

Reputation: 1289

According to the package example, you should use response.data.toString() if you want to print it.

If you want to use it, properly decode it first.

Upvotes: 0

Stijn2210
Stijn2210

Reputation: 884

Add “Content-Type”: “application/json” and “Accept”: “application/json” to the headers parameters of your dio.post() method

Upvotes: 1

Related Questions