WebView: intercepting outgoing POST request body.

GregoryD 1 Reputation point
2023-01-03T11:03:25.657+00:00

Hello,

I have a login web app embedded in a WebView that returns id tokens through a POST request which I would like to intercept and handle in my .net MAUI application. I am currently able to intercept the outgoing POST request by using the WebView Navigating event, but I am unable to retrieve the body of the request. WebNavigatingEventArgs doesn't seem to have any property that would contain this (see code snippet below).

   <WebView   
       Source="https://myLogin.WebApp?redirect_uri=https://example.com"  
       Navigating="OnWebViewNavigating"  
        />  
         
       ....  
         
       private void OnWebViewNavigating(object sender, WebNavigatingEventArgs e)  
       {  
           if (e.Url.StartsWith("https://example.com"))  
           {  
               e.Cancel = true;  
         
              //TODO get the POST request body and use the id token in my app  
                 
           }  
       }  

Am I proceeding correctly? If yes, how can I get the body of the POST request? Otherwise, how does one handle such a use case?

Thanks in advance.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,843 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,021 Reputation points Microsoft Vendor
    2023-01-05T05:50:41.013+00:00

    Hello,

    Is there any vision to normalize requests interception through the main, platform-agnostic MAUI library (I'm asking since WebViewClient.ShouldInterceptRequest is from Xamarin SDK)?

    No, this is because intercepting HTTP requests depends on the WebView's API on the specific platform.

    You could refer to Invoke platform code to get more details about how to access the required platform APIs.

    The following documentations would be helpful for you:

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.