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

urlencode doesn't match jQuery's ajax method when encoding an array of objects #367

Open
afontenot opened this issue Jul 14, 2023 · 0 comments
Labels
Enhancement Pull Request Welcome Project would be willing to review a pr to fix this

Comments

@afontenot
Copy link

When I pass an array of Javascript objects to .ajax like so:

const data = {
    "prefix": [
        {"a": 1, "b": 2, "c": 3},
        {"a": 4, "b": 5, "c": 6}
    ]
};

$.ajax({
 url: "https://httpbin.org/post",
 method: "POST",
 data: data
})

I see from the browser devtools that what I sent (in decoded form) looks like this:

prefix[0][a]: 1
prefix[0][b]: 2
prefix[0][c]: 3
prefix[1][a]: 4
prefix[1][b]: 5
prefix[1][c]: 6

In fact I can even leave the prefix off, using an empty string instead, and the result is the same (without "prefix").

Unfortunately this doesn't work at all with urlencode:

from requests_toolbelt.utils import formdata
data = {
    "prefix": [
        {"a": 1, "b": 2, "c": 3},
        {"a": 4, "b": 5, "c": 6}
    ]
}
formdata.urlencode(data)
ValueError: too many values to unpack (expected 2)

To the best of my understanding, doing this ought to work, since the original purpose of the urlencode tool is to enable using urlencoded forms that match jQuery and PHP's behavior with requests. IMO urlencode should handle arrays in jQuery fashion, just like it matches jQuery's behavior for the case of nested objects.

@sigmavirus24 sigmavirus24 added Enhancement Pull Request Welcome Project would be willing to review a pr to fix this labels Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Pull Request Welcome Project would be willing to review a pr to fix this
Projects
None yet
Development

No branches or pull requests

2 participants