I am learning Swift and I don\'t know how to send parameters to server using Swift.
In Objective-C we can do this by using \"%@\" as the placeholder.
But what s
Based on the above I ended up with this, to get a token within a Set-Cookie element.
Where the URLResponse was
{ URL: http://bla.co.uk//auth/authenticate?email=bob@isp.eu&password=xcode } { status code: 200, headers {
"Cache-Control" = "private, must-revalidate";
Connection = "keep-alive";
"Content-Type" = "application/json";
Date = "Fri, 17 Feb 2017 10:51:41 GMT";
Expires = "-1";
Pragma = "no-cache";
Server = nginx;
"Set-Cookie" = "token=Cu4CmOaverylongstring0mCu4CmOpBGg; expires=Fri, 17-Feb-2017 20:51:41 GMT; Max-Age=36000; path=auth; httponly";
"Transfer-Encoding" = Identity;
"X-Powered-By" = "PHP/5.5.9-1ubuntu4.19, PleskLin";
} }
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if statusCode == 200 {
let keyValues = httpResponse.allHeaderFields.map { (String(describing: $0.key).lowercased(), String(describing: $0.value)) }
// Now filter the array, searching for your header-key, also lowercased
if let myHeaderValue = keyValues.filter({ $0.0 == "Set-Cookie".lowercased() }).first {
print(myHeaderValue.1)
let cookies = myHeaderValue.1
let cookieDict = cookies.components(separatedBy: ";")
print("\(cookieDict)")
let tokenEntryParameter = cookieDict.filter({$0 .contains("token")})
let tokenEntry = tokenEntryParameter.first
token = (tokenEntry?.components(separatedBy: "=").last)!
}
}
}