Splunk Dev

What is the correct way to POST data to a custom endpoint and handle it in a python script ?

twesthead
Path Finder

Dear Splunk genie,

I have configured a custom REST endpoint with a Python script.
When I call my endpoint with fields in the query string I am able to read them in my Python script.
However, if I try to use the POST method with data in the request body, I can't access this data in my Python script.

What is the correct way to POST data to a custom endpoint and handle it in a python script ?

This is my code so far :

Below : my_app/local/web.conf (Holds a stanza with the name of my endpoint)

[endpoint:my_script]

Below : my_app/appserver/controllers/my_script.py (Holds my script, with name matching the endpoint)

import splunk.appserver.mrsparkle.controllers as controllers
from splunk.appserver.mrsparkle.lib.decorators import expose_page

class Controller(controllers.BaseController):
    @expose_page(must_login=False) 
    def my_function(self, **kwargs) :
        arg1 = kwargs.get('arg1', 'default_val_arg1')
        arg2 = kwargs.get('arg2', 'default_val_arg2')
        return arg1 + arg2

Now using a REST client.
GET on the following url returns "onetwo", which is good.

http://localhost:8000/en-US/custom/my_app/my_script/my_function?arg1=one&arg2=two

POST on the following url returns "default_val_arg1default_val_arg2"
With Content-type : application/x-www-form-urlencoded
And Body : arg1=one&arg2=two

http://localhost:8000/en-US/custom/my_app/my_script/my_function
0 Karma
1 Solution

twesthead
Path Finder

I fixed this by specifying the HTTP POST method in the decorators.

@expose_page(must_login=False, methods=['POST']) 

If you don't, the method defaults to GET and doesn't read parameters from the body.
Also don't forget to specify a locale like en-US in the URL or else you will be slapped by a 405 Method Not Allowed error.

View solution in original post

0 Karma

twesthead
Path Finder

I fixed this by specifying the HTTP POST method in the decorators.

@expose_page(must_login=False, methods=['POST']) 

If you don't, the method defaults to GET and doesn't read parameters from the body.
Also don't forget to specify a locale like en-US in the URL or else you will be slapped by a 405 Method Not Allowed error.

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...