@@ -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
0 commit comments