Ryuichi Aida
Ryuichi Aida

Reputation: 51

Can't set httpBody on WKWebView POST request in iOS11.3

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.

Upvotes: 2

Views: 5113

Answers (3)

AbdelAli
AbdelAli

Reputation: 816

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)
}

Upvotes: 5

BinaryGuy
BinaryGuy

Reputation: 1286

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.

Upvotes: 1

Konstantin Berkov
Konstantin Berkov

Reputation: 1212

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()

Upvotes: 2

Related Questions