flutter_inappwebview
flutter_inappwebview copied to clipboard
[Flutter - Android] Sending POST request with JSON body using `InAppWebView` results in 415 Unsupported Media Type
- [x] I have read the Getting Started section
- [x] I have already searched for the same problem
Environment
Technology | Version |
---|---|
Flutter version | 3.19.3 |
Plugin version | 6.0.0 |
Android version | |
iOS version | |
macOS version | |
Xcode version | |
Google Chrome version |
Device information:
- All Android emulators
- Samsung Galaxy A33 5G
- Samsung Galaxy Tab A8
Description
In my Flutter app, I want my logged-in user to be able to click on a button that opens an internal browser to access a server requiring an authentication, and I want to pass the user's JWT auth token in the request to avoid the user to authenticate to that server. I have read the Getting Started section I have the following configurations:
-
minSdkVersion 21
-
<uses-permission android:name="android.permission.INTERNET"/>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
- I created my app in summer 2023, so I guess I do not need to worry about migrating to AndroidX (do I?)
Expected behavior:
Using InAppWebView
to open an internal browser, configuring the initialUrlRequest
to send a POST request with a JSON content, I expect to have my request received by the server and successfully detected as having its Content-Type
set to application/json
.
Current behavior:
When opening the InAppWebView
, I get a page with a
415 Unsupported Media Type
Did not attempt to load JSON data because the request Content-Type was not 'application/json'.
The exact same code works for iOS (I get the expect web page), but not for Android (I get the 415)
Steps to reproduce
- Have a simple button opening the preconfigured
InAppWebView
sending an initial POST request with a server expecting for the answer - Notice it works on iOS but not on Android
...
ElevatedButton(
onPressed: () => Navigator.push(context,
MaterialPageRoute(builder: (_) => const CustomInAppBrowser()),
),
child: const Text('Open InAppWebView'),
)
class CustomInAppBrowser extends StatelessWidget {
const CustomInAppBrowser({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: const Icon(Icons.close),
onPressed: () {
Navigator.of(context).pop();
},
),
title: const Text('InAppWebView'),
),
body: InAppWebView(
initialUrlRequest: URLRequest(
url: WebUri('server_address_here/token'),
method: 'POST',
body: utf8.encode(
jsonEncode({"token": jwtAuthTokenAsStringHere}),
),
headers: {
'Content-Type': 'application/json',
},
),
onWebViewCreated: (controller) {},
),
);
}
}