Skip to content

Commit 444bce3

Browse files
committed
Support string body in the generic request method
Updates #577
1 parent 114c36a commit 444bce3

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

splunklib/binding.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,12 @@ def request(
938938
str(mask_sensitive_data(dict(all_headers))),
939939
mask_sensitive_data(body),
940940
)
941-
if body:
941+
942+
if isinstance(body, str):
943+
if method.upper() == "GET":
944+
raise Exception("unable to set string body on GET method request")
945+
message = {"method": method, "headers": all_headers, "body": body}
946+
elif body:
942947
body = _encode(**body)
943948

944949
if method == "GET":

tests/system/test_cre_apps.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from splunklib import results
2222

2323

24-
class TestJSONCustomRestEndpointsApp(testlib.SDKTestCase):
24+
class TestJSONCustomRestEndpointsSpecialMethodHelpers(testlib.SDKTestCase):
2525
app_name = "cre_app"
2626

2727
def test_GET(self):
@@ -75,3 +75,66 @@ def test_DELETE(self):
7575
"method": "DELETE",
7676
},
7777
)
78+
79+
80+
class TestJSONCustomRestEndpointGenericRequest(testlib.SDKTestCase):
81+
app_name = "cre_app"
82+
83+
def test_no_str_body_GET(self):
84+
def with_body():
85+
self.service.request(
86+
app=self.app_name, method="GET", path_segment="execute", body="str"
87+
)
88+
89+
self.assertRaisesRegex(
90+
Exception, "unable to set string body on GET method request", with_body
91+
)
92+
93+
def test_GET(self):
94+
resp = self.service.request(
95+
app=self.app_name,
96+
method="GET",
97+
path_segment="execute",
98+
headers=[("x-bar", "baz")],
99+
)
100+
self.assertIn(("x-foo", "bar"), resp.headers)
101+
self.assertEqual(resp.status, 200)
102+
self.assertEqual(
103+
json.loads(str(resp.body)),
104+
{
105+
"headers": {"x-bar": "baz"},
106+
"method": "GET",
107+
},
108+
)
109+
110+
def test_POST(self):
111+
self.method("POST")
112+
113+
def test_PUT(self):
114+
self.method("PUT")
115+
116+
def test_PATCH(self):
117+
self.method("PATCH")
118+
119+
def test_DELETE(self):
120+
self.method("DELETE")
121+
122+
def method(self, method: str):
123+
body = json.dumps({"foo": "bar"})
124+
resp = self.service.request(
125+
app=self.app_name,
126+
method=method,
127+
path_segment="execute",
128+
body=body,
129+
headers=[("x-bar", "baz")],
130+
)
131+
self.assertIn(("x-foo", "bar"), resp.headers)
132+
self.assertEqual(resp.status, 200)
133+
self.assertEqual(
134+
json.loads(str(resp.body)),
135+
{
136+
"payload": '{"foo": "bar"}',
137+
"headers": {"x-bar": "baz"},
138+
"method": method,
139+
},
140+
)

0 commit comments

Comments
 (0)