Reputation: 123
I am new to Python, what I am trying to do is write a script that will take the IP address I am supplying it and updating an array that I am pulling from a JSON RESTful API. I can pull the data out of the array fine. Here is what my code looks like so far(please excuse the look of the code)
import requests
import json
import sys
pool_name = sys.argv[1]
add_node = sys.argv[2]
url = 'https://<stingray RESTApi>:9070/api/tm/1.0/config/active/pools/' + pool_name
jsontype = {'content-type': 'application/json'}
client = requests.Session()
client.auth = ('<username>', '<password>')
client.verify = 0
response = client.get(url)
pools = json.loads(response.content)
nodes = pools['properties']['basic']['nodes']
Now I have been looking at using this
client.put(url, <I am stuck>, headers = jsontype)
At this point I have reached the limits of my current know how of Python(As I just started to learn on the last few days). I have looked at using something like this as well to get the data I have collected append to the array and then trying to PUT it.
updatepool['properties']['basic']['nodes'].append(add_node)
When I print updatepool, I see that what I am after is working but again PUTing it in the array has stumped me.
Any help would be really appreciated.
Thanks
UPDATE: Here is an update to my code, getting a 400 response from the API
#!/usr/bin/python
import requests
import json
import sys
pool_name = sys.argv[1]
#add_node = sys.argv[2]
add_node = u'10.10.10.1:80'
url = 'https://<uri>:9070/api/tm/1.0/config/active/pools/' + pool_name
jsontype = {'content-type': 'application/json'}
client = requests.Session()
client.auth = ('<username>', '<password')
client.verify = 0
response = client.get(url)
pools = json.loads(response.content)
nodes = pools['properties']['basic']['nodes']
data = nodes
data.append(add_node)
print client.put(url,json.dumps(data), headers=jsontype)
Upvotes: 2
Views: 1618
Reputation: 89
From the docs
print requests.put.doc Sends a PUT request. Returns :class:
Response
object.
:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
so client.put(url, {'key':'value'}, headers = jsontype)
works.
what you need to know now is what key val that url is accepting: supposing it accepts a 'node' and key you could use
client.put(url, {'node':add_node}, headers = jsontype)
or
client.put(url, {'node':updatepool['properties']['basic']['nodes']**[0]**}, headers = jsontype)
to send first node
Upvotes: 0
Reputation: 21317
As per documentation
put(url, data=None, **kwargs)
Sends a PUT request. Returns Response object.
Parameters:
url – URL for the new Request object.
data – (optional) Dictionary, bytes, or file-like object to send in the body of the Request.
**kwargs – Optional arguments that request takes.
You can give 'dictwith
list` (array).
Upvotes: 0