-
Notifications
You must be signed in to change notification settings - Fork 46
DCNM_REST: Add support for Encoding Type 'x-www-form-urlencoded' #462
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -636,7 +636,7 @@ def dcnm_image_upload_handle_local_file_transfer(self, elem): | |
if file_path: | ||
upload_files = {"file": open(file_path, "rb")} | ||
|
||
resp = dcnm_post_request(path, headers, False, upload_files) | ||
resp = dcnm_post_request(path, headers, verify=False, files=upload_files) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you test this fix with the |
||
|
||
self.result["response"].append(resp) | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -44,6 +44,12 @@ | |||
- json_data | ||||
required: no | ||||
type: raw | ||||
encoding: | ||||
description: | ||||
- 'Type of body encoding to use for the request' | ||||
choices: ['json', 'text', 'urlencoded'] | ||||
default: json | ||||
type: str | ||||
author: | ||||
- Mike Wiebe (@mikewiebe) | ||||
""" | ||||
|
@@ -91,8 +97,7 @@ | |||
import json | ||||
from ansible.module_utils.basic import AnsibleModule | ||||
from ansible_collections.cisco.dcnm.plugins.module_utils.network.dcnm.dcnm import ( | ||||
dcnm_send, | ||||
) | ||||
dcnm_send, dcnm_post_request, dcnm_login_retrieve_token) | ||||
|
||||
|
||||
def main(): | ||||
|
@@ -101,6 +106,7 @@ def main(): | |||
method=dict(required=True, choices=["GET", "POST", "PUT", "DELETE"]), | ||||
path=dict(required=True, type="str"), | ||||
data=dict(type="raw", required=False, default=None, aliases=["json_data"]), | ||||
encoding=dict(type="str", default="json", choices=["json", "text", "urlencoded"]), | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer we follow the same method we used for |
||||
) | ||||
|
||||
# seed the result dict | ||||
|
@@ -116,14 +122,30 @@ def main(): | |||
break | ||||
if data is None: | ||||
data = "{}" | ||||
encoding = module.params["encoding"] | ||||
|
||||
# Determine if this is valid JSON or not | ||||
try: | ||||
json.loads(data) | ||||
result["response"] = dcnm_send(module, method, path, data) | ||||
if encoding == "urlencoded": | ||||
headers = {} | ||||
auth_token = dcnm_login_retrieve_token(module) | ||||
headers.update(auth_token) | ||||
headers.update({'Content-Type': 'application/x-www-form-urlencoded'}) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not need to login directly here in this module. Can you explore handling this as another option like we do for See: ansible-dcnm/plugins/httpapi/dcnm.py Line 59 in 238c026
|
||||
resp = dcnm_post_request(path, headers, data=data, verify=False) | ||||
result["response"] = resp | ||||
elif encoding == "json": | ||||
result["response"] = dcnm_send(module, method, path, data) | ||||
else: | ||||
msg = "Encoding type '{}' is not supported for input data".format(encoding) | ||||
module.fail_json(msg=msg) | ||||
except json.JSONDecodeError: | ||||
# Resend data as text since it's not valid JSON | ||||
result["response"] = dcnm_send(module, method, path, data, "text") | ||||
if encoding == "text": | ||||
result["response"] = dcnm_send(module, method, path, data, "text") | ||||
else: | ||||
msg = "Encoding type '{}' is not supported for input data".format(encoding) | ||||
module.fail_json(msg=msg) | ||||
|
||||
if result["response"]["RETURN_CODE"] >= 400: | ||||
module.fail_json(msg=result["response"]) | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, but why was this changed to use kwargs? Mainly readability or was it a functional problem you were fixing here?