'How to get the HTTP response from WebVIew in Flutter?

I'm trying to sign in to an app via WebView, and at the end of the process it gives me a response with a token. How can I get the response data from the WebView? Any workarounds for this?

The example of the response



Solution 1:[1]

You can launch request url in a webview and inject javascript to get response. Example code using flutter webview plugin:

final webView = FlutterWebviewPlugin();
webView.launch(requestUrl); //hidden: true if necessary
webView.onStateChanged.listen((event) async {
  if (event.type == WebViewState.finishLoad) {
    final response = await webView
      .evalJavascript("document.documentElement.innerText");
    print(response);
  }
);

Solution 2:[2]

Using webview_flutter you can get your response by injecting javascript like that:

 late WebViewController _controller;

your webview widget

 WebView(
         initialUrl: YOUR_URL,
         javascriptMode: JavascriptMode.unrestricted,
          onWebViewCreated: (controller) {
           _controller = controller;
         },
        onPageFinished: (finish) {
          //reading response on finish
          final response = await _controller.runJavascriptReturningResult("document.documentElement.innerText");
          print(jsonDecode(response)); //don't forget to decode into json
         },
       ),



           

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Irfan Akram