0
votes

When making a call to an API, I receive this as response:

<NSHTTPURLResponse: 0x7f96f0510f80> { URL: https://insula.magister.net/api/sessie } { status code: 201, headers {
    "Cache-Control" = "max-age=120, private";
    "Content-Length" = 0;
    Date = "Thu, 30 Jul 2015 17:27:11 GMT";
    "Set-Cookie" = "SESSION_ID=c8714acb-22f8-4295-90ee-82e9469890e1; domain=insula.magister.net; path=/api; secure; httponly";
    "Strict-Transport-Security" = "max-age=31536000";
    Vary = "Accept-Encoding,Cookie";
    "X-Frame-Options" = DENY;
} }

print(HTTPResponse.allHeaderFiles) prints the following output:

[Set-Cookie: SESSION_ID=65a59001-de75-41ff-ad22-70549baa96f5; domain=insula.magister.net; path=/api; secure; httponly, Strict-Transport-Security: max-age=31536000, Date: Thu, 30 Jul 2015 17:33:13 GMT, X-Frame-Options: DENY, Content-Length: 0, Cache-Control: max-age=120, private, Vary: Accept-Encoding,Cookie]

But I can't get cookiesWithResponseHeaderFields to work, I should pass a [String:String] array as first parameter, but the response doesn't seem to meet.

let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(HTTPResponse.allHeaderFields, forURL: url)

But as HTTPResponse.allHeaderFiles is an array of type [NSObject:AnyObject] it can't meet the [String:String] parameter type. Who knows what to do?

1

1 Answers

2
votes

There's a similar question with a great answer.

You should cast the allHeaderFields property to the type you need, that is [String:String], like this:

  let foundCookies: [NSHTTPCookie]?
  if let responseHeaders = HTTPResponse.allHeaderFields as? [String:String] {
    foundCookies = NSHTTPCookie.cookiesWithResponseHeaderFields(responseHeaders, forURL:url)
  }