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

interceptRequest method #138

Open Tary-nan opened 8 months ago

Tary-nan commented 8 months ago

Is it possible to retrieve the request body in the interceptRequest method with version 2.0.0-beta.7 ?

CodingAleCR commented 8 months ago

Yes it is possible. You can use the example to guide your interceptor for that. The basic idea is that you take the body, and add whatever you want to it and then return it within the request

Tary-nan commented 8 months ago

I'll show you an example. What I want to do is create a request interceptor which is responsible for encrypting the body of my application's requests and then decrypting it when the data is received in the response.

class EncryptDataInterceptor extends InterceptorContract {
  EncryptDataInterceptor(this.local, this.user);
  final Logger log = Logger();

  final PublicKeyLocalDataSource local;
  final AuthLocalDataSource user;
  final isEncrypt = bool.tryParse('${GlobalParams.IS_ENCRYPT_VALUE}');

  @override
  Future<BaseRequest> interceptRequest({required BaseRequest request}) async {
    final headers = Map<String, String>.from(request.headers);

    headers[HttpHeaders.contentTypeHeader] = 'application/json';

    // When I want to encrypt my data
    if (isEncrypt != null && isEncrypt! == true) {
      final currentUser = await user.read();
      final publicKeyValue = await local.read();
      headers['isEncrypte'] = 'true';
      //////
      // 1 - I want to encrypt the body of my request using AES
      // 2 - I'm having a problem because I can't access the body
      // 3 - I get this error when I try to access the body ``The getter 'body' : sn't defined for the type 'BaseRequest```''
      // 4 - Where else can I get the body of my request in the interceptor?
      log.i(
        "Ex : ${request.body} The getter 'body' : sn't defined for the type 'BaseRequest'.",
      );
     //////

      if (currentUser?.sessionUser != null) {
        headers['sessionUser'] = currentUser!.sessionUser!;
      }

      final aes = EncryptData.encryptAES(request.body as String);
    }

    return request.copyWith(
      headers: headers,
      body: aes,
    );
  }
  @override
  Future<BaseResponse> interceptResponse({
    required BaseResponse response,
  }) async {
    if (response is Response) {
      if (isEncrypt != null && isEncrypt! == true) {
      //////
      // 1 - I decrypt the answer using AES
      //////
        final body = json.encode(utf8.decode(response.bodyBytes));
        if ((body as Map).containsKey('item')) {
          response.copyWith(body: json.encode(EncryptData.decryptAES(body)));
        }
      }
    }
    return response;
  }
}
MuTe33 commented 7 months ago

@Tary-nan You have to cast the BaseRequest. Depending on your request type it's either a StreamedRequest, Request or MultiPartRequest.

Example:

@override
  Future<BaseRequest> interceptRequest({required BaseRequest request}) async {
     if(request is StreamedRequest){
           request.body
     }

     // ... other cases

}