Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interceptRequest method #138

Open
Tary-nan opened this issue Nov 22, 2023 · 3 comments
Open

interceptRequest method #138

Tary-nan opened this issue Nov 22, 2023 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@Tary-nan
Copy link

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

@Tary-nan Tary-nan added the question Further information is requested label Nov 22, 2023
@CodingAleCR
Copy link
Owner

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
Copy link
Author

Tary-nan commented Nov 23, 2023

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;
  }
}

@MuTe33
Copy link

MuTe33 commented Dec 17, 2023

@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

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants