pichillilorenzo / flutter_inappwebview

A Flutter plugin that allows you to add an inline webview, to use a headless webview, and to open an in-app browser window.
https://inappwebview.dev
Apache License 2.0
3.31k stars 1.64k forks source link

Cannot intercept POST request using androidShouldInterceptRequest #578

Closed divyanshub024 closed 3 years ago

divyanshub024 commented 4 years ago

Environment

Technology Version
Flutter version 1.22.3
Plugin version 4.0.0+4

Description

Current behavior:

Cannot intercept POST request using androidShouldInterceptRequest . All the requests I'm getting are just the GET method. I tried this on chrome browser and I can see post request after login. I tried with multiple sites but the same issue, can't get POST request. I assume all the requests are intercepting but they are coming as GET request.

Also, I can't get form data in the post request. How can I get it?

Expected behavior:

Intercept the POST request.

Code

import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:offline_kyc/home_page.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await FlutterDownloader.initialize(
      debug: true // optional: set false to disable printing logs to console
      );
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: NewHomePage(),
    );
  }

class NewHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: InAppWebView(
        initialUrl: 'https://www.linkedin.com/',
        initialOptions: InAppWebViewGroupOptions(
          crossPlatform: InAppWebViewOptions(
            debuggingEnabled: true,
            useOnDownloadStart: true,
            useShouldOverrideUrlLoading: true,
          ),
          android: AndroidInAppWebViewOptions(
            useShouldInterceptRequest: true,
            saveFormData: true,
          ),
        ),
        onWebViewCreated: (controller) {},
        shouldOverrideUrlLoading: (
          controller,
          ShouldOverrideUrlLoadingRequest shouldOverrideUrlLoadingRequest,
        ) async {
          print('shouldOverrideUrlLoading: $shouldOverrideUrlLoadingRequest');
          return ShouldOverrideUrlLoadingAction.ALLOW;
        },
        androidShouldInterceptRequest: (
          controller,
          WebResourceRequest request,
        ) async {
          print('androidShouldInterceptRequest: $request');
          return null;
        },
      ),
    );
  }
}
rymesaint commented 3 years ago

same with me i can't detect post method

pichillilorenzo commented 3 years ago

Unfortunately, this is a limitation of the Android native WebView. Check this open issue in the Google issue tracker about it: https://issuetracker.google.com/issues/36918490 However, as people are already doing, I think you can manage it injecting custom JavaScript code and intercept JavaScript events and send data between dart and JavaScript side with “JavaScript Handlers”. Also, if you need to intercept ajax and fetch requests, you can use the shouldInterceptAjaxRequest and shouldInterceptFetchRequest events.

github-actions[bot] commented 1 month ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.