URLRequest cookies are always empty

Viewed 356

When trying to retrieve the cookies from anywhere I can't parse the cookies. The storage seems to be always empty. Maybe I'm just fundamentally misunderstanding the whole thing.

I start off by doing a get or post request against my endpoint via a shared URLSession task (for example: https://httpbin.com/cookies)

If I get a valid response status I try to parse the cookies (Note that response is not nil and everything works fine here, I just didn't include that code because i don't think its relevant):

func setCookies(response: URLResponse) {
  let response = response as! HTTPURLResponse
  let cookies = HTTPCookie.cookies(withResponseHeaderFields:response.allHeaderFields as! [String : String], for: response.url!)
  print("cookies: \(cookies) \(HTTPCookieStorage.shared.cookies!)")
}

For both of these methods my result is always empty. Even though the header fields are set: enter image description here

I already did set cookieAcceptPolicy to .alwaysand httpShouldHandleCookies to true

Is there something else I'm missing? How is it possible for both of them to be empty?

0 Answers
Related