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

interceptRequest method

Tary-nan opened this issue · comments

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

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

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.

  • I want to encrypt the body of my request using AES
  • I'm having a problem because I can't access the body
  • I get this error when I try to access the body <<The getter 'body' : sn't defined for the type 'BaseRequest>>
  • Where else can I get the body of my request in the interceptor?
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;
  }
}

@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

}