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
Cannot make URL query string with a parameter without a value #2651
Comments
I can see some value in this. For API reasons it could only ever work with the 'list of tuples' approach, but I'd be ok with us adding support for this. @sigmavirus24? |
I don't think we use it (yet) but it seems that urllib3 uses If we examine how that behaves, you can see that it doesn't like either of the proposed ways of working with this. >>> u = urlparse.urlparse('http://example.com/foo?bar')
>>> u
ParseResult(scheme='http', netloc='example.com', path='/foo', params='', query='bar', fragment='')
>>> urlparse.parse_qs(u.query)
{}
>>> urllib.urlencode({'foo': None})
'foo=None'
>>> urllib.urlencode([('foo',)])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 1336, in urlencode
for k, v in query:
ValueError: need more than 1 value to unpack |
Related: "urlencode of a None value uses the string 'None'" – https://bugs.python.org/issue18857 |
Give @piotr-dobrogost's input, @agilevic have you tried using the empty string as the value there? Does it work with your API server? |
Bump. =) |
>>> import requests
>>> r = requests.get('https://httpbin.org/get', params={'foo': ''})
>>> r.request.url
'https://httpbin.org/get?foo=' |
It's not the same as having a parameter without a value. Yours renders the = sign after the parameter name. Some applications will work, but as a matter of providing a complete solution this exact case should be addressed. That urllib doesn't do it is of no significance. Requests does many things better than standard libraries for HTTP - that is its reason to exist. |
@agilevic What would your proposed API design for this feature be? |
Here is a crazy thought:
That is what I think should be happening. What is actually happening:
:( |
@frnhr So the reason that doesn't work with our API is that setting a key to |
I'm afraid I don't see what the session object has to do with this bit of API, sincerely. But ok, maybe
|
@frnhr The So we certainly could do it, but I'm not sure to what extent it's worthwhile. When not using key-value mapping, you should just pass a string to the |
What if we were to make a specific typed argument that could indicate to requests that the param is to be added without a value?
Its not None, its not a scalar constant... so it should be backwards compatible. Under the hood... we could check for that value type and specially append this parameter to the constructed url. |
So while that will definitely work, I don't think that API will get past Kenneth. |
Yeah, No. I'd rather support a built-in like |
|
That would be a pretty major change though, and I don't think it would benefit many people. Perhaps an empty tuple could be considered. (e.g. |
Not sure if this was resolved, but I found this thread trying to do the exact thing of adding a key with out a value. "QueryAll" in my case, but I have had a number of causes in my RestAPI Automation to make use of this kind of function. |
what happens if you pass |
I get &QueryAll= at the end. |
Looks like it's kinda up to the API on how it handles the open "=" then, PasswordState's API took it as long as it was at the end of my params list, if I moved it to the beginning it error-ed. |
in 3.0, i think we can consider making emptry string not result in an = |
That would be great :-D So far my Passwordstate project can move forwward with the QueryAll= format at the end of teh params string so I am back on track. I'll watch this thread :-D Thanks Kenneth! |
Have not heard or seen anything yet, but have not circled back around to see if any updates had been released.
Nick
From: Alex Zagoro <notifications@github.com>
Sent: Saturday, September 22, 2018 10:19 AM
To: requests/requests <requests@noreply.github.com>
Cc: Ellson, Nick <NEllson@columbia.com>; Comment <comment@noreply.github.com>
Subject: Re: [requests/requests] Cannot make URL query string with a parameter without a value (#2651)
heya, any updates on this?
|
I'm running into this problem at the moment as well. Has anyone else found any way around this problem? |
I believe there is still no solution in requests for this simple, but very common feature. weird. |
Unfortunately facing the same issue. @Lukasa You seem to know the codebase better than most of us, what do you think about it? Wouldn't that resolve in a non-breaking-change, @kennethreitz? Defaults could be set to mimic 2.x behaviour. |
It appears that this is still adding the = when an empty string is passed in. Time to bang out a work-around! |
So I'm guessing this is still a problem? |
If you have parameters that have no value, you can list them as part of the url and any additional parameters will append with
|
How about this rule?
|
2020... and still an issue.... SMH |
This comment has been minimized.
This comment has been minimized.
What's the solution folks? |
URL query string may contain a parameter, which has no value i.e. http://host/path/?foo or http://host/path/?a=1&foo. Currently Requests does not provide support for that.
Expected:
The text was updated successfully, but these errors were encountered: