4

In our WKWebView, we have a multi part form POST request which we need to inspect and conditionally handle.

Currently, we're trying this using the WKNavigationDelegate's webView:decidePolicyForNavigationAction:decisionHandler: method to gain access to the NSURLRequest. (navigationAction.request).

But when we inspect the request here, we can verify that it is the multi part form POST, however, the [request HTTPBody] returns nil.

abc123
  • 8,043
  • 7
  • 49
  • 80

3 Answers3

2

Althrough i didn't found a docu for this, my shot would be that for security reasons the body in the request is empty, or assigned later than in

webView:decidePolicyForNavigationAction:decisionHandler:

Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
1

Unfortunately it is the bug in WebKit :(( :

For some cases mentioned workaround from Florent Crivello can be used (https://bugs.webkit.org/show_bug.cgi?id=145410#c14):

NSString *javascriptPOSTRedirect = @"\
var form = document.createElement('form');\
form.method = 'POST';\
form.action = '<URL>';\
\
var input = document.createElement('input');\
input.type = 'text';\
input.name = '<key>';\
input.value = '<value>';\
form.appendChild(input);\
form.submit();";

[webView evaluateJavaScript:javascriptPOSTRedirect completionHandler:^(id _Nullable content, NSError * _Nullable error) {
    // Your thing
}];
JMI
  • 2,530
  • 15
  • 26
0

The request might contain a body stream. If so, and if you can modify the URL request, you could potentially read from that stream, then replace the request with a new one that uses a body data object.

Failing that, I think the only way to handle it would be to register a custom protocol handler that checks to see if it needs to handle it, and if so, handles it, and if not, either refuses it (if you can detect it without reading from the stream) or reissues it with some sort of tag that you can recognize (to avoid your protocol handler touching it the next time).

dgatwood
  • 10,129
  • 1
  • 28
  • 49
  • Try a protocol handler, then. – dgatwood Nov 02 '15 at 18:26
  • Right. The intent of WKWebView was to make protocol handlers unnecessary, but WKWebView isn't fully baked, so it fails to do so. The solution is to A. file bugs with Apple to get them to fix WKWebView. B. Use UIWebView, where NSURLProtocol is supported and should handle this situation easily, and C. adopt WKWebView only in future iOS versions if and when that API fully works. – dgatwood May 27 '16 at 18:09
  • Incidentally, I just added a comment in the underlying WebKit bug at https://bugs.webkit.org/show_bug.cgi?id=145410 requesting that they add body streams. We'll see what happens. But file a Radar anyway. – dgatwood May 27 '16 at 18:19