Skip to content
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

Space at end of query string is trimmed #223

Open
Laerte opened this issue Mar 11, 2024 · 1 comment
Open

Space at end of query string is trimmed #223

Laerte opened this issue Mar 11, 2024 · 1 comment

Comments

@Laerte
Copy link
Member

Laerte commented Mar 11, 2024

Faced this issue while writing a spider, basically if we don't percent-encoding the space or only have one parameter the space is trimmed (if we have more parameters but the one with space is at end its also trimmed).

Find below the snippet:

from w3lib.url import safe_url_string
from urllib.parse import parse_qsl, urlparse

url = safe_url_string("https://httpbin.org/anything?keyword=A ")
assert (dict(parse_qsl(urlparse(url).query))["keyword"] == "A ") is False

url = safe_url_string("https://httpbin.org/anything?keyword=A%20")
assert (dict(parse_qsl(urlparse(url).query))["keyword"] == "A ") is True

url = safe_url_string("https://httpbin.org/anything?keyword=A &dummy=value")
assert (dict(parse_qsl(urlparse(url).query))["keyword"] == "A ") is True

url = safe_url_string("https://httpbin.org/anything?keyword=A%20&dummy=value")
assert (dict(parse_qsl(urlparse(url).query))["keyword"] == "A ") is True

url = safe_url_string("https://httpbin.org/anything?dummy=value&keyword=A ")
assert (dict(parse_qsl(urlparse(url).query))["keyword"] == "A ") is False

url = safe_url_string("https://httpbin.org/anything?dummy=value&keyword=A%20")
assert (dict(parse_qsl(urlparse(url).query))["keyword"] == "A ") is True
@Gallaecio
Copy link
Member

I think the current behavior is OK for scenarios where you want a behavior consistent with that of a web browser, i.e. with what would happen if you pasted that URL in the address bar of a web browser.

Maybe we should have different functions for the different behaviors. Not sure how to call them, though.

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

2 participants