Skip to content

Conversation

anotheren
Copy link

Motivation

I use a custom ClientMiddleware to inject cookie for some APIs, but I found the final Request always lost some cookies.

https://github.com/apple/swift-http-types/blob/e6359663d11a5c7d2340ac81f2e025d5c5fb0e14/Sources/HTTPTypes/HTTPFields.swift#L190

The reason is that there may be more than one cookie in the HTTPRequest's headerFields. Before setting them to the URLRequest, you must join them together. Otherwise, only the last cookie will be effective due to simple overwriting.

Modifications

Check header.name before setting it to the URLRequest

Result

All cookies now work well.

Test Plan

Not yet.

}
self.init(url: url)
self.httpMethod = request.method.rawValue
for header in request.headerFields { setValue(header.value, forHTTPHeaderField: header.name.canonicalName) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it also get fixed if we used the addValue API instead? https://developer.apple.com/documentation/foundation/urlrequest/2011522-addvalue

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the addValue API might be a better option. ChatGPT suggested something related to Cookie and Set-Cookie headers, but I didn’t find any relevant information in the official documentation. I’m not sure if there might be other issues involved here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addValue not work 😂

image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addValue use , but cookie needs ; 💔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guoye-zhang can you help here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #51 and borrowed the code to do the correct transaction from swift-http-types

@czechboy0
Copy link
Contributor

@anotheren let's close this and use #51, thanks for pushing for this fix! 👏

@czechboy0 czechboy0 closed this Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants