<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to handle posted data in custom endpoint? in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/How-to-handle-posted-data-in-custom-endpoint/m-p/270539#M3399</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;

&lt;P&gt;I'm quite new to splunk and I'm stuck somewhere...&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;&lt;P&gt;My goal:&lt;BR /&gt;
Sending data from a Splunk view (a webpage I created from the splunk web interface), process some fields in a custom REST endpoint and store them in the KV store.&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;What I have:&lt;BR /&gt;
I already have the custom endpoint working, I can handle in py python script flat javascript value received but not enclosed objects.&lt;/P&gt;&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;Javascript (attached to a basic html form created in splunk interface):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    ...
    data['name'] = aJsStringValue; // I can handle this in pyhton
    data['list'][0] = aJsObject; // I failed

    var service = mvc.createService(); 
    service.post('/services/test', data, function(err, response) { 
    ...
    }
    ...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Python:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    ...
    class test(splunk.rest.BaseRestHandler):
        def handle_POST(self):
            try:
                ...
                name = ''
                list = []

                # parse the payload
                payload = self.request['payload']
                for el in payload.split('&amp;amp;'):
                     key, value = el.split('=')
                     if 'name' in key:
                        name = value
                     if 'list' in key:
                        # idk
                ...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;UL&gt;
&lt;LI&gt;My problem:
I want to send a list of dictionaries inside my javascript object but I don't know how to handle it in python. 
I wanted to use Json and put a String directly as "params" value for the "service.post()" but it is obviously interpreted as an array.
I could do a workaround, use "JSON.stringify(data);" and place the string in a simple field "data" but I would like to know if a better way exists?&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;Note: I can change all the structure if needed.&lt;/P&gt;

&lt;P&gt;Thanks. &lt;/P&gt;</description>
    <pubDate>Tue, 31 Jan 2017 15:05:25 GMT</pubDate>
    <dc:creator>nori_t</dc:creator>
    <dc:date>2017-01-31T15:05:25Z</dc:date>
    <item>
      <title>How to handle posted data in custom endpoint?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-handle-posted-data-in-custom-endpoint/m-p/270539#M3399</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;

&lt;P&gt;I'm quite new to splunk and I'm stuck somewhere...&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;&lt;P&gt;My goal:&lt;BR /&gt;
Sending data from a Splunk view (a webpage I created from the splunk web interface), process some fields in a custom REST endpoint and store them in the KV store.&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;What I have:&lt;BR /&gt;
I already have the custom endpoint working, I can handle in py python script flat javascript value received but not enclosed objects.&lt;/P&gt;&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;Javascript (attached to a basic html form created in splunk interface):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    ...
    data['name'] = aJsStringValue; // I can handle this in pyhton
    data['list'][0] = aJsObject; // I failed

    var service = mvc.createService(); 
    service.post('/services/test', data, function(err, response) { 
    ...
    }
    ...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Python:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    ...
    class test(splunk.rest.BaseRestHandler):
        def handle_POST(self):
            try:
                ...
                name = ''
                list = []

                # parse the payload
                payload = self.request['payload']
                for el in payload.split('&amp;amp;'):
                     key, value = el.split('=')
                     if 'name' in key:
                        name = value
                     if 'list' in key:
                        # idk
                ...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;UL&gt;
&lt;LI&gt;My problem:
I want to send a list of dictionaries inside my javascript object but I don't know how to handle it in python. 
I wanted to use Json and put a String directly as "params" value for the "service.post()" but it is obviously interpreted as an array.
I could do a workaround, use "JSON.stringify(data);" and place the string in a simple field "data" but I would like to know if a better way exists?&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;Note: I can change all the structure if needed.&lt;/P&gt;

&lt;P&gt;Thanks. &lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2017 15:05:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-handle-posted-data-in-custom-endpoint/m-p/270539#M3399</guid>
      <dc:creator>nori_t</dc:creator>
      <dc:date>2017-01-31T15:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle posted data in custom endpoint?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-handle-posted-data-in-custom-endpoint/m-p/270540#M3400</link>
      <description>&lt;P&gt;I couldn't find the ready solution for that, so created my own method - you need to extract both payload and content-type to pass it in:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def parsePayload(contentType, payload):
    posted_parts = {}
    if re.match('application/x-www-form-urlencoded', contentType):
        posted_parts = urlparse.parse_qs(payload)
    elif re.search('form-data', contentType):
        # First, determine the separator (boundary)
        parsedBoundary = re.search('boundary\s*=\s*(?P&amp;lt;boundary&amp;gt;\S+)', contentType)
        boundary = parsedBoundary.group('boundary')
        # Now, split the payload
        # posted_parts = urlparse.parse_qs(payload)
        parts = payload.split('--' + boundary)
        for part in parts:
            try:
                parsedPart = re.search('Content-Disposition: form-data; name="(?P&amp;lt;name&amp;gt;[^"]+)"(\r?\nContent-Type:[^\r\n]*)?(\r?\nContent-Length:[^\r\n]*)?\r?\n\r?\n(?P&amp;lt;content&amp;gt;.*)', part)
                posted_parts[parsedPart.group('name')] = parsedPart.group('content').strip()
                # posted_parts.append({ 'name' : parsedPart.group('name'), 'value' : parsedPart.group('content') })
            except:
                pass
    return posted_parts
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Apr 2017 20:09:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-handle-posted-data-in-custom-endpoint/m-p/270540#M3400</guid>
      <dc:creator>arkadyz1</dc:creator>
      <dc:date>2017-04-21T20:09:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle posted data in custom endpoint?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-handle-posted-data-in-custom-endpoint/m-p/270541#M3401</link>
      <description>&lt;P&gt;If the code is completely working, please mark your answer as accepted so the question will be shown as answered.  Thanks for posting the solution!&lt;/P&gt;</description>
      <pubDate>Fri, 21 Apr 2017 21:46:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-handle-posted-data-in-custom-endpoint/m-p/270541#M3401</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-04-21T21:46:48Z</dc:date>
    </item>
  </channel>
</rss>

