I am trying to load a URL that redirects. It works fine on Mac OS, but when I push it to Bluemix using Kitura 1.4 server, Swift 3.0.2 I get the following load error:
The operation could not be completed
Here is my relevant code:
…
let gShort = URL(string: "https://blabla.bl/blabla")! // Because SO don't want shortened urls in the example
var request = URLRequest(url: gShort, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 3)
request.httpMethod = "HEAD"
let dataTask = URLSession(configuration: URLSessionConfiguration.default)
.dataTask(with: request, completionHandler: { data, loadResponse, error in
guard let redirected = loadResponse?.url else {
…
}
})
dataTask.resume()
I don't actually care about the response; I just want the redirected URL. That's why I do "HEAD", but "GET" has the same behaviour, i.e. works fine on Mac OS but doesn't work on Bluemix.
I haven't figured out a workaround. I don't know if it's a bug in Swift / Foundation, or Kitura, or if Bluemix blocks redirected urls.
Anyone know how to fix that?
(This could've been a comment, but I still don't have enough reputation!)
Yes, redirection support is yet to be implemented in Swift Foundation. There is a related bug report opened quite a while ago - https://bugs.swift.org/browse/SR-2678