2

following code

var webView: WKWebView!
override func loadView() {
 super.loadView()
 self.webView = WKWebView(frame: .zero)
 view = webView
}
override func viewDidLoad() {
 super.viewDidLoad()
 var request = URLRequest(url: URL(string: "https://hosts")!)
 request.httpMethod = "POST"
 let bodyData = "data1=postData"
 request.httpBody = bodyData.data(using: .utf8)!
 self.webView.load(request)
}

I was able to get the POST parameter on Web site loaded on iOS 11.2, but could not get it on iOS 11.3

Why did not get it on iOS 11.3? Was the specification changed?

I want anyone to tell me the workaround. Please give me the answer.

asked Jul 13, 2018 at 10:45

3 Answers 3

5

WKWebView doesn’t automatically set the Content-Type header to application/x-www-form-urlencoded for POST requests the way UIWebView does.

var webView: WKWebView!
override func loadView() {
 super.loadView()
 self.webView = WKWebView(frame: .zero)
 view = webView
}
override func viewDidLoad() {
 super.viewDidLoad()
 var request = URLRequest(url: URL(string: "https://hosts")!)
 request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
 request.httpMethod = "POST"
 let bodyData = "data1=postData"
 request.httpBody = bodyData.data(using: .utf8)!
 self.webView.load(request)
}
answered Mar 26, 2020 at 16:37
Sign up to request clarification or add additional context in comments.

Comments

2

Haven't encountered this error on 11+, but had it surfaced on 10.3.3, was OK on 12+ though. I've reverted to performing requests with bodies, manually using shared session, than inserting loaded data into WKWebView.

var firstRequest: URLRequest = URLRequest(url: requestUrl)
firstRequest.httpMethod = "POST"
firstRequest.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
firstRequest.httpBody = "data1=\(requestData1)".data(using: .utf8)
URLSession.shared.dataTask(with: firstRequest) { [weak self] (data, response, error) in
 // check error, response and data to show useful error message
 if let data = data {
 self?.webView.load(body, mimeType: "text/html", characterEncodingName: "UTF-8", baseURL: baseUrl)
 }
}.resume()
answered Nov 13, 2018 at 10:36

Comments

1

If you switch to WKWebView you can use the following approach. Basically you need to make use of WKURLSchemeHandler protocol:

1. Setup webview

let config = WKWebViewConfiguration()
config.setURLSchemeHandler(self, forURLScheme: "my-custom-scheme")
let wkWebView = WKWebView(frame: .zero, configuration: config)

2. Implement WKURLSchemeHandler protocol

func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
 if let url = urlSchemeTask.request.url, url.scheme == "my-custom-scheme" {
 self.handleTermURLCallback(urlSchemeTask.request)
 }
}

By using this you'll find that your request httpBody in the callback is not null and you can continue the same way you used to with UIWebView.

It took a lot of effort to find this but it is working for us and we didn't need to change very much. Hope it helps you guys.

answered Mar 23, 2020 at 0:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.