asmodeoux
asmodeoux

Reputation: 419

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

Upvotes: 2

Views: 5308

Answers (2)

Irfan Akram
Irfan Akram

Reputation: 868

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



           

Upvotes: 2

ertgrull
ertgrull

Reputation: 1298

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

Upvotes: 6

Related Questions