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

URL parameter is added when the value is an empty string but not when it is an empty list #6557

Open
Lalee10 opened this issue Oct 25, 2023 · 2 comments

Comments

@Lalee10
Copy link

Lalee10 commented Oct 25, 2023

Both an empty string '' and an empty list [] are not of NoneType and are considered a falsy value and therefore should behave the same when being added as a URL parameter. But in requests when passing an empty string '' as a parameter value the final URL contains the parameter but not when an empty list [] is passed as the value.

Expected Result

When passing an empty list as parameter value, the final URL should contain the parameter.

Actual Result

When passing an empty list as parameter value, the final URL does not contain the parameter.

Reproduction Steps

import requests

base_url = 'https://jsonplaceholder.typicode.com/todos'

params = {'_limit': 5, 'id__in': ''}
response = requests.get(base_url, params)
print(response.request.url)
# Output: https://jsonplaceholder.typicode.com/todos?_limit=5&id__in=

params = {'_limit': 5, 'id__in': []}
response = requests.get(base_url, params)
print(response.request.url)
# Output: https://jsonplaceholder.typicode.com/todos?_limit=5

System Information

$ python -m requests.help
{
  "chardet": {
    "version": null
  },
  "charset_normalizer": {
    "version": "3.1.0"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "3.4"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.10.11"
  },
  "platform": {
    "release": "22.6.0",
    "system": "Darwin"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.28.2"
  },
  "system_ssl": {
    "version": "1010115f"
  },
  "urllib3": {
    "version": "1.26.15"
  },
  "using_charset_normalizer": true,
  "using_pyopenssl": false
@Krupakar-Reddy-S
Copy link

Yes as observed this is the case when using any other empty datatype, except a str(), as it gives an empty string.

From Documentation:
" Note that any dictionary key whose value is None will not be added to the URL’s query string. "

But as known, an empty list is not of None type, so i guess since it does not know what to do with empty datatypes it just treats then as None.

@amkarn258
Copy link
Contributor

Proposed a fix here - #6561

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
@Lalee10 @amkarn258 @Krupakar-Reddy-S and others