Skip to content

urequests.request(): unique headers only (dict keys) #615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gnbl opened this issue Feb 16, 2023 · 7 comments
Closed

urequests.request(): unique headers only (dict keys) #615

gnbl opened this issue Feb 16, 2023 · 7 comments

Comments

@gnbl
Copy link

gnbl commented Feb 16, 2023

A web site I am attempting to scrape sends multiple "set-cookie"s.

These appear to be written to a dictionary, i.e. previous keys ("Set-Cookie") are overwritten:
https://github.com/micropython/micropython-lib/blob/master/python-ecosys/urequests/urequests.py#L153

A custom function can be supplied - here's one that keeps a list:

def ph(l, resp_d):
    l = str(l, "utf-8")
    k, v = l.split(":", 1)
    if k in resp_d:
        resp_d[k] = [resp_d[k], v.strip()]
    else:
        resp_d[k] = [v.strip()]

But this of little use since the headers dict parameter of the request function has the same limitation (only one can be specified).

@andrewleech
Copy link
Contributor

Do you mean you also want to be able to make a request with multiple set-cookie headers?
Not just parse them from a response?

This duplicate header issue is discussed pretty regularly, both in regular python and here (#217 (comment))

@gnbl
Copy link
Author

gnbl commented Feb 17, 2023

It is my understanding that for multiple cookies, multiple cookie headers are required.
So to be able to handle sessions, both, parsing multiple set-cookie from a response and requesting multiple cookies is needed.
But it seems you are well aware of this limitation - sorry I did not see that. Feel free to close as duplicate.

@gnbl
Copy link
Author

gnbl commented Feb 17, 2023

Ah, it seems cookies are concatenated via semicolon (Wikipedia), so for the request, one cookie header is sufficient

@gnbl
Copy link
Author

gnbl commented Feb 17, 2023

This concatenates the above array of response set-cookies into the request cookie:

cookie = "; ".join( c.split(";")[0] for c in r.headers["Set-Cookie"] )

"Works for me!" ;-) Now I need to figure out how to parse 50 kB of HTML..

@gnbl
Copy link
Author

gnbl commented Feb 17, 2023

redirect drops arguments, if I see that correctly: https://github.com/micropython/micropython-lib/blob/master/python-ecosys/urequests/urequests.py#L163

@jonnor
Copy link

jonnor commented Aug 25, 2024

It seems that a fix was found. So I believe this issue can be closed?

@projectgus
Copy link
Contributor

Agreed. @gnbl I'm not sure about the meaning of your last comment, but if it looks like a bug then please open a new issue for it and we can take a look. 🙏

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

No branches or pull requests

4 participants