I want to open a URL with WKWebView
by passing some parameters along in the URL as a post request. But i am unable to do it. Please help.
Asked
Active
Viewed 8,276 times
1

yaakov
- 5,552
- 35
- 48

Kapil Rathore
- 82
- 1
- 10
-
1"by passing some parameters along in the URL as a post request" POST parameters don't go in the URL. Please clarify. – yaakov May 23 '16 at 11:24
-
1Not actually a duplicate but this might help you: http://stackoverflow.com/q/26253133/653513 – Rok Jarc May 23 '16 at 11:28
-
i meant something like https://example.com?email=krkapil@gmail.com&pass=password – Kapil Rathore May 23 '16 at 11:54
-
i tried that example but its not working – Kapil Rathore May 23 '16 at 11:55
-
@KapilRathore The example you shown was a GET request example. As @codran stated, POST parameters don't go in the URL. If you need to send POST, via `WKWebView`, [this answer](http://stackoverflow.com/a/1143020/1492173) might help you. – Yevhen Dubinin May 23 '16 at 13:44
-
@EugeneDubinin. That answer uses UIWebView, not WKWebView. – Mike Taverne May 23 '16 at 23:27
-
@MikeTaverne it does not matter in this case - `WKWebView` has similar method: `- (nullable WKNavigation *)loadRequest:(NSURLRequest *)request` where the `NSMutableURLRequest` from aforementioned answer could be submitted to. – Yevhen Dubinin May 24 '16 at 09:15
-
3@EugeneDubinin Yes, it does matter. The answer you referred to sets HTTPBody which unfortunately does not work for WKWebView due to this bug https://bugs.webkit.org/show_bug.cgi?id=145410 – Mike Taverne May 24 '16 at 18:25
-
@MikeTaverne that actually change everything related to the subject! The discussion on the bug gives some workaround with JavaScript. I did't double-check it myself, though. Meanwhile, from discussion it wasn't clear to me whether or not someone's gonna fix it any time soon. – Yevhen Dubinin May 25 '16 at 13:40
-
This will help you: https://stackoverflow.com/questions/26253133/cant-set-headers-on-my-wkwebview-post-request/44951545#44951545 – OhadM Jul 06 '17 at 14:25
-
Possible duplicate of [Can't set headers on my WKWebView POST request](https://stackoverflow.com/questions/26253133/cant-set-headers-on-my-wkwebview-post-request) – Ozgur Vatansever Oct 26 '17 at 08:20
2 Answers
1
var request = new NSMutableUrlRequest(new NSUrl(new NSString(paymentwebview.url))); //Your Url
request.HttpMethod = "POST";
request.Body = NSData.FromString(paymentwebview.data); //Data for POST
request["Content-Length"] = request.Body.Length.ToString();
request["Content-Type"] = "application/x-www-form-urlencoded charset=utf-8";
LoadRequest(request);

Piotr Labunski
- 1,638
- 4
- 19
- 26

LIJU DANIEL
- 88
- 8
-2
Had a similar problem. What worked for me was passing the parameters as cookies.
Code example:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod: @"POST"];
NSString *params = @"param1=value1;param2=value2;";
[request addValue:params forHTTPHeaderField:@"Cookie"];
[self.webView loadRequest:request];
Hope it helps!

chents
- 393
- 3
- 14