Skip to content

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

@Lalee10

Description

@Lalee10

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions