4444 - json_data
4545 required: no
4646 type: raw
47+ urlencoded_data:
48+ description:
49+ - 'Dictionary data to be url-encoded for x-www-form-urlencoded type REST API call'
50+ required: no
51+ type: raw
4752author:
4853 - Mike Wiebe (@mikewiebe)
4954"""
6671 path: /rest/top-down/fabrics/fabric1/vrfs/attachments
6772 json_data: '[{"vrfName":"sales66_vrf1","lanAttachList":[{"fabric":"fabric1","vrfName":"sales66_vrf1","serialNumber":"FDO21392QKM","vlan":2000,"freeformConfig":"","deployment":false,"extensionValues":"","instanceValues":"{\" loopbackId\" :\" \" ,\" loopbackIpAddress\" :\" \" ,\" loopbackIpV6Address\" :\" \" }"}]}]'
6873
74+ - name: Save Robot Credentials - (urlencoded)
75+ dcnm_rest:
76+ method: POST
77+ path: /rest/lanConfig/saveRobotCredentials
78+ urlencoded_data: '{"password": "password", "username": "admin"}'
79+
6980# Read payload data from file and validate a template
7081- set_fact:
7182 data: "{{ lookup('file', 'validate_payload') }}"
89100"""
90101
91102import json
103+ import urllib .parse
92104from ansible .module_utils .basic import AnsibleModule
93105from ansible_collections .cisco .dcnm .plugins .module_utils .network .dcnm .dcnm import (
94106 dcnm_send ,
@@ -101,6 +113,7 @@ def main():
101113 method = dict (required = True , choices = ["GET" , "POST" , "PUT" , "DELETE" ]),
102114 path = dict (required = True , type = "str" ),
103115 data = dict (type = "raw" , required = False , default = None , aliases = ["json_data" ]),
116+ urlencoded_data = dict (type = "raw" , required = False , default = None ),
104117 )
105118
106119 # seed the result dict
@@ -110,17 +123,28 @@ def main():
110123
111124 method = module .params ["method" ]
112125 path = module .params ["path" ]
113- for key in ["json_data" , "data" ]:
126+ is_urlencoded = False
127+
128+ for key in ["json_data" , "data" , "urlencoded_data" ]:
114129 data = module .params .get (key )
115130 if data is not None :
131+ if key == "urlencoded_data" :
132+ is_urlencoded = True
116133 break
117134 if data is None :
118135 data = "{}"
119136
120137 # Determine if this is valid JSON or not
121138 try :
122- json .loads (data )
123- result ["response" ] = dcnm_send (module , method , path , data )
139+ json_data = json .loads (data )
140+ if is_urlencoded :
141+ # If the data is valid JSON but marked as urlencoded, we need to convert it
142+ # to a URL-encoded string before sending it.
143+ urlencoded_data = urllib .parse .urlencode (json_data )
144+ result ["response" ] = dcnm_send (module , method , path , urlencoded_data , "urlencoded" )
145+ else :
146+ # If the data is valid JSON, send it as a JSON string
147+ result ["response" ] = dcnm_send (module , method , path , data )
124148 except json .JSONDecodeError :
125149 # Resend data as text since it's not valid JSON
126150 result ["response" ] = dcnm_send (module , method , path , data , "text" )
0 commit comments