Skip to content

Commit 2248e27

Browse files
committed
Fix cookies using async/await API
1 parent 92285d9 commit 2248e27

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

templates/swift/Sources/Client.swift.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ open class Client {
275275
func complete(with response: HTTPClientResponse) async throws -> T {
276276
switch response.status.code {
277277
case 0..<400:
278-
if response.headers["cookies"].count > 0 {
278+
if response.headers["Set-Cookie"].count > 0 {
279279
UserDefaults.standard.set(
280-
try! response.headers["cookies"].toJson(),
280+
response.headers["Set-Cookie"],
281281
forKey: URL(string: request.url)!.host! + "-cookies"
282282
)
283283
}

templates/swift/Sources/Extensions/HTTPClientRequest+Cookies.swift.twig

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,11 @@ extension HTTPClientRequest {
1717

1818
extension HTTPHeaders {
1919
public mutating func addDomainCookies(for domain: String) {
20-
let cookieJson = UserDefaults.standard.string(forKey: "\(domain)-cookies")
21-
let cookies: [HTTPClient.Cookie?]? = try? cookieJson?.fromJson(to: [HTTPClient.Cookie].self)
22-
?? [(try? cookieJson?.fromJson(to: HTTPClient.Cookie.self))]
23-
24-
var cookiesValue = ""
25-
for cookie in cookies ?? [] {
26-
if let cookie = cookie {
27-
cookiesValue += "\(cookie.name)=\(cookie.value);"
28-
}
20+
guard let cookies = UserDefaults.standard.stringArray(forKey: "\(domain)-cookies") else {
21+
return
22+
}
23+
for cookie in cookies {
24+
add(name: "Cookie", value: cookie)
2925
}
30-
add(name: "Cookie", value: cookiesValue)
3126
}
3227
}

0 commit comments

Comments
 (0)