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.13k stars 1.45k forks source link

InterceptAjaxRequest can't add header #2220

Open norman93sun opened 1 month ago

norman93sun commented 1 month ago

Environment

Technology Version
Flutter version 3.22.2
Plugin version
Android version
iOS version 17.4
macOS version
Xcode version
Google Chrome version

Device information:

Description

Expected behavior: Here I want to use shouldInterceptAjaxRequest and change the request url and add header.

Current behavior: Only request url be changed, header not working at all.

Steps to reproduce

I submit the source code https://github.com/norman93sun/flutter_inappwebview, you can run the flutter_inappwebview example I init webview with initialData: InAppWebViewInitialData(data: htmlString, baseUrl: WebUri('http://localhost'))

shouldInterceptAjaxRequest:

shouldInterceptAjaxRequest: (controller, ajaxRequest) async {
                    print('ajaxRequest ---------- ${ajaxRequest.toString()}');

                    /// change https://form.mcttechnology.cn/shtest-prod/shtest-0-ccn-uploadfiles-en/storage/s3
                    if (ajaxRequest.url.toString().contains('storage/s3')) {
                      ajaxRequest.url = WebUri.uri(Uri.parse(
                          'https://shtest-app-v2.mcttechnology.cn/api/platform/formio/shtest-0-ccn-uploadfiles-en/storage/s3'));

                      ajaxRequest.headers?.setRequestHeader('authorization',
                          'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6Ikl2VyJ9.eyJpc3MiOm51bGwsInN1YiI6MTExMTY1MjQxLCJpYXQiOjE3MjA1ODE3MTMsImV4cCI6MTcyMDYyNDkxMywiZW1haWwiOiIzMjY1NjQ0NTYxQHFxLmNvbSIsImdpdmVuX25hbWUiOiJzaHRlc3QiLCJmYW1pbHlfbmFtZSI6ImNqanl5IiwibmFtZSI6IjMyNjU2NDQ1NjFAcXEuY29tIiwicHJlZmVycmVkX3VzZXJuYW1lIjoiMzI2NTY0NDU2MUBxcS5jb20iLCJqdGkiOjQsInVzZXJJZCI6bnVsbH0.uU0WVtEJoa3v17_UHKzaee1y2cFXdArvKQidylPa-YY');
                    }

                    return ajaxRequest;
                  },

Step: upload a image by tap browse. The get error.

If I dont InterceptAjaxRequest evrything go right like below: 截屏2024-07-10 15 45 32

But got 401 error when I changed the request url, but the authorization header I passed not working. 截屏2024-07-10 15 44 56

I suspect it's a CORS problem, so how to fix this?

github-actions[bot] commented 1 month ago

👋 @norman93sun

NOTE: This comment is auto-generated.

Are you sure you have already searched for the same problem?

Some people open new issues but they didn't search for something similar or for the same issue. Please, search for it using the GitHub issue search box or on the official inappwebview.dev website, or, also, using Google, StackOverflow, etc. before posting a new one. You may already find an answer to your problem!

If this is really a new issue, then thank you for raising it. I will investigate it and get back to you as soon as possible. Please, make sure you have given me as much context as possible! Also, if you didn't already, post a code example that can replicate this issue.

In the meantime, you can already search for some possible solutions online! Because this plugin uses native WebView, you can search online for the same issue adding android WebView [MY ERROR HERE] or ios WKWebView [MY ERROR HERE] keywords.

Following these steps can save you, me, and other people a lot of time, thanks!