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

Ability to edit Response body in interceptor #124

Open mohamed155 opened 1 year ago

mohamed155 commented 1 year ago

Since the HTTP Response body is a getter, we need a way to modify the Response body after it's fetched.

For example:

` class APIInterceptors extends InterceptorContract { @override Future interceptRequest({required RequestData data}) async { return data; }

@override Future interceptResponse({required ResponseData data}) async { data.body = someMethodMakeSomeModifications(data.body); return data; } } `

CodingAleCR commented 1 year ago

This is a great idea. I'm looking into it, as it would enable body parsing, but it's a complex operation and there may be some features that get affected and some even might get deprecated.

Manasmd commented 1 year ago

Yes, this would be great!

I actually tried overriding the response body, however, it's not working:


    @override
     Future<ResponseData> interceptResponse({required ResponseData data}) async {

    ResponseData newData = data;

    newData.body = '{"test":"test"}';

    print('response data has been modified...');
    print(newData.body);
    return newData;
  }
CodingAleCR commented 1 year ago

Sorry, what version of the package are you using @mohamed155 ?

I made some tests for this in the beta releases and just want to double-check if the tests are not covering this scenario for some reason.

stale[bot] commented 10 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

mohamed155 commented 8 months ago

Hi @CodingAleCR، i'm using the version 1.0.2

CodingAleCR commented 1 week ago

@mohamed155 There were a couple of fixes included into beta versions of 2.0.0 that might have not been replicated to 1.x versions. In any case there's a stable version 2.x that could fix your issues and improve a bunch of other things.