Categories
nativePHP

Handling Android WebView Form Submissions in NativePHP

Today I ran into an interesting issue with NativePHP when submitting forms from an Android WebView. Unlike normal browsers, WebView handles POST requests slightly differently, and $request->all() may return an empty array, even when the form has valid data.

Here’s an example Blade view:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Form Test</title>
</head>
<body>
<form method="POST" action="/login">
    @csrf
    <div>
        <label for="username">Username:</label>
        <input type="text" id="username" name="username">
    </div>
    <div>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password">
    </div>
    <button type="submit">Submit</button>
</form>
</body>
</html>

On Android WebView, submitting this form results in:

$request->all(); // []

The Solution
To handle this, you need to use $request->getContent(), which retrieves the raw POST body, and parse it.

use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;

public function store(Request $request): RedirectResponse
{
    // Collect form data (normal browsers / iOS)
    $postData = $request->all();

    // Android WebView sends raw POST body, parse it manually
    if (System::isAndroid()) {
        parse_str($request->getContent(), $androidData);
        $postData = array_merge($postData, $androidData);
    }

    // Now $postData contains all form fields reliably
    // Process your data as normal
}

Leave a Reply

Your email address will not be published. Required fields are marked *