Repository navigation
ill-formed Set-Cookie header cause KeyError #3526
Replies: 1 comment
|
The response is sending a malformed cookie header: "Set-Cookie: foo" has no "=" or cookie value. RFC 6265 §4.1.1 defines a cookie pair as "cookie-name "=" cookie-value". If the intended cookie has an empty value, the server should send: Set-Cookie: foo= HTTPX's "Cookies" mapping raises "KeyError" here because the malformed cookie is parsed with a "None" value, and "Cookies.getitem()" raises when "get()" returns "None". If you can't change the server and need to inspect the parsed cookies, iterate over the underlying cookie jar instead: for cookie in resp.cookies.jar: So the primary fix is to correct the server's "Set-Cookie" header. The iteration workaround lets you inspect the cookie without going through the mapping's "items()" implementation. See "RFC 6265" (https://www.rfc-editor.org/rfc/rfc6265.html#section-4.1.1) and "HTTPX's cookie implementation" (https://github.com/encode/httpx/blob/master/httpx/_models.py). |
Uh oh!
There was an error while loading. Please reload this page.
when 'Set-Cookie' header is ill-formed, got
KeyErroron iterating cookies.is this intended behavior or an httpx bug?
versions: (httpx was installed by
pip install httpx)step to reproduce:
All reactions