CodingAleCR / http_interceptor

A lightweight, simple plugin that allows you to intercept request and response objects and modify them if desired.
MIT License
133 stars 67 forks source link

Problem while logging request and response #131

Closed akabisdev closed 1 year ago

akabisdev commented 1 year ago

Hello, I recently migrated my flutter project from version 3.7.12 to version 3.10.5,

In the process I had to update http and http_interceptor dependencies as well.

So currently I am using

http: 1.1.0
http_interceptor: 2.0.0-beta.7

I have added logger interceptor :

import 'dart:developer';

import 'package:http_interceptor/http_interceptor.dart';

class LoggerInterceptor extends InterceptorContract {
  @override
  Future<BaseRequest> interceptRequest({
    required BaseRequest request,
  }) async {
    log('----- Request -----');
    log(request.url.toString());
    log(request.toString());
    log(request.headers.toString());
    return request;
  }

  @override
  Future<BaseResponse> interceptResponse({
    required BaseResponse response,
  }) async {
    log('----- Response -----');
    log('Code: ${response.statusCode}');
    log(response.toString());
    return response;
  }
}

With the above LoggerInterceptor, the output is as follows : image

I have 2 queries, 1) The request body is not printed in the console, how can we print request body as well along with URL and Headers? 2) While logging response in the console, it is printing Instance of Response, how can we print response.body ?

Any suggestions

Thanks

ChoyCheeWei commented 1 year ago

I have the same issue also

akabisdev commented 1 year ago

I have the same issue also

Did you find any workaround?

jaehyung0 commented 1 year ago

I have the same issue. No solution to get response body?

ChoyCheeWei commented 1 year ago

I have the same issue also

Did you find any workaround?

nope

CodingAleCR commented 1 year ago

Hi, thank you all for reporting this. I'll take a look at it and get back as soon as I can. 🙏🏼

CodingAleCR commented 1 year ago

Hey all!

The reason you are getting this as the log of the response is that Response does not have a custom implementation for toString(). This means that the current result: "Instance of Response" is actually the right string representation of the class.

Now, regarding logging response body you can do so in multiple ways but the easier one for me is checking for types using is operator like so:

@override
Future<BaseResponse> interceptResponse({
  required BaseResponse response,
}) async {
  log('----- Response -----');
  log('Code: ${response.statusCode}');
  if (response is Response) {
    log((response).body);
  }
  return response;
}

I've updated the example and the docs to reflect this suggestion for you all.

¡Pura vida! ~ Alejandro.

ChoyCheeWei commented 1 year ago

possible to log the param also?