Python Request Containing Parameters That Have Values And Parameters That Do Not Have Values
Solution 1:
You have two options with Requests in terms of the query string: 1) provide key/value pairs by way of a dictionary, or 2) provide a string. If you provide #1, you'll always get a '=' for each key/value pair...not what you want. So you have to use #2, which will let you do whatever you want, since it will just include what you give it as the entire query string. The downside is that you have to construct the query string yourself. There are many ways of doing that, of course. Here's one way:
params = {'param1': 'value1', 'param2': None}
params = '&'.join([k if v is None else f"{k}={v}"for k, v inparams.items()])
r = requests.get('https://example.com/service', params=params)
print(r.url)
This way lets you supply a dictionary, just like if you were letting Requests build the query string, but it allows you to specify a value of Null
to indicate that you want just the key name, with no '='. Requests will normally not include the parameter at all if its value is None
in the dictionary.
The result of this code is exactly what you gave as an example of what you want:
https://example.com/service?param1=value1¶m2
Post a Comment for "Python Request Containing Parameters That Have Values And Parameters That Do Not Have Values"