Skip to content

Commit d7c1258

Browse files
committed
Support string body in the generic request method
Updates #577
1 parent 818a948 commit d7c1258

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-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: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323

2424
@pytest.mark.smoke
25-
class TestJSONCustomRestEndpointsApp(testlib.SDKTestCase):
25+
class TestJSONCustomRestEndpointsSpecialMethodHelpers(testlib.SDKTestCase):
2626
app_name = "cre_app"
2727

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

0 commit comments

Comments
 (0)