CodingAleCR / http_interceptor

A lightweight, simple plugin that allows you to intercept request and response objects and modify them if desired.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem while logging request and response

akabisdev opened this issue · comments

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

I have the same issue also

I have the same issue also

Did you find any workaround?

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

I have the same issue also

Did you find any workaround?

nope

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

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.

possible to log the param also?