Skip to content

Commit 6e6d38b

Browse files
author
Frederick Ross
committed
Context.post supports GET-style arguments with unstructured POST body as well as normal POST.
This is needed for the receivers/simple and receivers/stream endpoints. Made Index.submit use this capability as it simplifies the code.
1 parent dc9561e commit 6e6d38b

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

‎splunklib/binding.py‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def get(self, path_segment, owner=None, app=None, sharing=None, **query):
541541

542542
@_authentication
543543
@log_duration
544-
def post(self, path_segment, owner=None, app=None, sharing=None, **query):
544+
def post(self, path_segment, owner=None, app=None, sharing=None, headers=[], **query):
545545
"""POST to *path_segment* with the given namespace and query.
546546
547547
Named to match the HTTP method. This function makes at least
@@ -553,12 +553,21 @@ def post(self, path_segment, owner=None, app=None, sharing=None, **query):
553553
``Context``'s default namespace. All other keyword arguments
554554
are included in the URL as query parameters.
555555
556+
Some of Splunk's endpoints, such as receivers/simple and
557+
receivers/stream, require unstructured data in the POST body
558+
and all metadata passed as GET style arguments. If you provide
559+
a ``body`` argument to ``post``, it will be used as the POST
560+
body, and all other keyword arguments will be passed as
561+
GET-style arguments in the URL.
562+
556563
:raises AuthenticationError: when a the ``Context`` is not logged in.
557564
:raises HTTPError: when there was an error in POSTing to *path_segment*.
558565
:param path_segment: A path_segment to POST to.
559566
:type path_segment: string
560567
:param owner, app, sharing: Namespace parameters (optional).
561568
:type owner, app, sharing: string
569+
:param headers: A dict or list of (key,value) pairs to use as headers for
570+
this request.
562571
:param query: All other keyword arguments, used as query parameters.
563572
:type query: values should be strings
564573
:return: The server's response.
@@ -589,7 +598,14 @@ def post(self, path_segment, owner=None, app=None, sharing=None, **query):
589598
path = self.authority + self._abspath(path_segment, owner=owner,
590599
app=app, sharing=sharing)
591600
logging.debug("POST request to %s (body: %s)", path, repr(query))
592-
response = self.http.post(path, self._auth_headers, **query)
601+
if isinstance(headers, dict):
602+
all_headers = [(k,v) for k,v in headers.iteritems()]
603+
elif isinstance(headers, list):
604+
all_headers = headers
605+
else:
606+
raise ValueError("headers must be a list or dict (found: %s)" % headers)
607+
all_headers += self._auth_headers
608+
response = self.http.post(path, all_headers, **query)
593609
return response
594610

595611
@_authentication
@@ -903,10 +919,18 @@ def get(self, url, headers=None, **kwargs):
903919
def post(self, url, headers=None, **kwargs):
904920
if headers is None: headers = []
905921
headers.append(("Content-Type", "application/x-www-form-urlencoded")),
922+
# We handle GET-style arguments and an unstructured body. This is here
923+
# to support the receivers/stream endpoint.
924+
if 'body' in kwargs:
925+
body = kwargs.pop('body')
926+
if len(kwargs) > 0:
927+
url = url + UrlEncoded('?' + encode(**kwargs), skip_encode=True)
928+
else:
929+
body = encode(**kwargs)
906930
message = {
907931
'method': "POST",
908932
'headers': headers,
909-
'body': encode(**kwargs)
933+
'body': body
910934
}
911935
return self.request(url, message)
912936

‎splunklib/client.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,8 +1572,7 @@ def submit(self, event, host=None, source=None, sourcetype=None):
15721572
# is that we are not sending a POST request encoded using
15731573
# x-www-form-urlencoded (as we do not have a key=value body),
15741574
# because we aren't really sending a "form".
1575-
path = UrlEncoded(PATH_RECEIVERS_SIMPLE + "?" + urllib.urlencode(args), skip_encode=True)
1576-
self.service.request(path, method="POST", body=event)
1575+
self.service.post(PATH_RECEIVERS_SIMPLE, body=event, **args)
15771576
return self
15781577

15791578
# kwargs: host, host_regex, host_segment, rename-source, sourcetype

‎tests/test_binding.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,25 @@ def test_update_user(self):
199199
self.assertEqual(entry.content.realname, "Renzo")
200200
self.assertEqual(entry.content.email, "email.me@now.com")
201201

202+
def test_post_with_body_behaves(self):
203+
self.test_create_user()
204+
response = self.context.post(
205+
PATH_USERS + self.username,
206+
body="defaultApp=search",
207+
)
208+
self.assertEqual(response.status, 200)
209+
210+
def test_post_with_get_arguments_to_receivers_stream(self):
211+
text = 'Hello, world!'
212+
response = self.context.post(
213+
'/services/receivers/simple',
214+
headers={'x-splunk-input-mode': 'streaming'},
215+
source='sdk', sourcetype='sdk_test',
216+
body=text
217+
)
218+
self.assertEqual(response.status, 200)
219+
220+
202221
class TestSocket(BindingTestCase):
203222
def test_socket(self):
204223
socket = self.context.connect()

0 commit comments

Comments
 (0)