WKWebView ignores NSURLRequest body

Hello,


I'm trying to migrate my code to WKWebView, and I have the following case:

NSAssert(parameters.length > 0); // just to be sure

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];

[self.webView loadRequest:request];


It works fine with UIWebView, but for WKWebView it sends a POST request with empty body.


I tried to add a policy decision handler for a WKWebView:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    NSURLRequest *request = navigationAction.request;

    // request.HTTPBody is nil here

    if ([request.HTTPMethod isEqualToString:@"POST"] && request.HTTPBody == nil && [request isKindOfClass:[NSMutableURLRequest class]])
    {
        // and even overwriting the body doesn't make it sent to server
        [(NSMutableURLRequest*)request setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];
    }

    decisionHandler(WKNavigationActionPolicyAllow);
}


WKWebView is created with a default configuration. Tested in both 9.0 and 8.4 simulators, and 9.0 device.

Any suggestions? Is there some policy that prevents a WKWebView being initialized with a POST request, or is it a bug that survived since iOS 8?


I understand there's hardly a lot of people who need to initialize a web view with POST requests, but still?..

Thanks,

Alex.

It’s likely that this worked with UIWebView as an accident of the implementation. WKWebView, OTOH, does all of its networking out of process, so such accidents are rare (everything that traverses the inter-process gap has to be explicitly coded to do so).

Having said that, the documentation doesn’t specifically state that web views must be initialised with a GET, so you’re not doing anything wrong here.

I recommend you create a small test app and attach it to a bug. Please post your bug number, just for the record.

As to a workaround, can you run the POST request yourself (using NSURLSession, say) and then initialise the web view with the response? Or run the POST request from within the web view using JavaScript?

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I'm try run the POST request witnin the web uising JavaScript.

I intercepted the request in NSURLProtocol, but HttpBody is still empty.

It seems through the WKWebView run the Post request is always empty body.

Is this a bug or something else?

WKWebView runs all of its networking out of process, so your NSURLProtocol shouldn’t see any requests generated by the web view. NSURLProtocol and WKWebView are pretty much incompatible; if you need to an NSURLProtocol to incept requests made by a web view, you’ll have to stick with UIWebView.

My question, however, is why you’re trying to do this? WKWebView has much better native/JavaScript integration facilities than UIWebView, so many of the older techniques (like NSURLProtocol) are no longer necessary.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I have the same issue.

In fact ,WKWebView/JavascriptCore can't cover all sences.

For example, I want upgrape the http to https ,if use the NSURLProtocol to do this ,just regist a URLProtocol change the http scheme to https scheme ,can cover all requset to https. But javascriptCore can't do this.


In my demo ,I take a native http request load by WKWebView ,also will ignore the body. This is not consistent with common sense.

I don't understand.

I also experience this problem. I have filed a bug report, case 29754094.

This is not consistent with common sense.

Be that as it may, all I can do is explain how WKWebView works. If you want it to work differently, feel free to file an enhancement request describing your requirements. However, you’ll still need to find a solution that works for you an current systems.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Does anybody have a solution for that problem. We have the same bug in iOS 11.3. In iOS 11.2. is everything fine. The postbody is not empty, only in iOS 11.3 is the postbody emtpy.

Hello,
Does anyone have the solution for WKWebView load request with post body?
I have same problem as mentioned above. I have checked it with iOS 13.5 version but I didn't get request loaded.
However, the same codebase with UIWebView is working fine.

Appreciated if any help!

WKWebView ignores NSURLRequest body
 
 
Q