Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 949
asmodeoux Avatar asked Nov 25 '25 05:11

asmodeoux


2 Answers

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);
  }
);
like image 113
ertgrull Avatar answered Nov 28 '25 00:11

ertgrull


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
         },
       ),



           
like image 24
Irfan Akram Avatar answered Nov 28 '25 00:11

Irfan Akram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!