I have another basic Pythonic question that I cant seem to figure out. Here we go. So what I am trying to accomplish is simply using a second variable in my "scanjob" variable adding "api_tok". I am using a product that needs an api_token for every call so I just want to persistently add "api_tok" where needed. So far
auths = requests.get('http://10.0.0.127:443', auth=('admin', 'blahblah'), headers = heads)
api_tok = {'api_token=e27e901c196b8f0399bc79'}
scanjob = requests.get('http://10.0.0.127:4242/scanjob/1?%s' % (api_tok))
scanjob.url
u"http://10.0.0.127:4242/scanjob/1?set(['api_token=e27e901c196b8f0399bc79'])"
as you can see from scanjob.url, its adding a "set" after the "?". Why? If I could remove that "set" my call will work. I tried many different variants of combining a string such as:
scanjob = requests.get('http://10.0.0.127:4242/scanjob/1?%s' + api_tok)
scanjob.url
u"http://10.0.0.127:4242/scanjob/1?set(['api_token=e27e901c196b8f0399bc79'])"
scanjob = requests.get('http://10.0.0.127:4242/scanjob/1?' + str(api_tok))
scanjob.url
u"http://10.0.0.127:4242/scanjob/1?set(['api_token=e27e901c196b8f0399bc79'])"
??
---------- Post updated at 05:14 PM ---------- Previous update was at 04:50 PM ----------
many thanks for your reply but I was able to figure it out with some help:
{....} is syntax to produce a set object:
As of Python 2.7, non-empty sets \(not frozensets\) can be created by placing a comma-separated list of elements within braces, for example: \{'jack', 'sjoerd'\}, in addition to the set constructor.
For example:
>>> {'api_token=e27e901c196b8f0399bc79'}
set(['api_token=e27e901c196b8f0399bc79'])
>>> {'jack', 'sjoerd'}
set(['jack', 'sjoerd'])
This is where your mystery set([...]) text comes from.
You just wanted to produce a string here:
api_tok = 'api_token=e27e901c196b8f0399bc79'
scanjob = requests.get('http://10.0.0.127:4242/scanjob/1?%s' % api_tok)
Alternatively, tell requests to add the query parameters with the params keyword argument, passing in a dictionary:
parameters = {'api_token': 'e27e901c196b8f0399bc79'}
scanjob = requests.get('http://10.0.0.127:4242/scanjob/1', params=parameters)
This has the added advantage that requests now is responsible for properly encoding the query parameters.