Splunk Dev

RESTful api output_mode of POST

benjaminws
Explorer

I see that I can set the output_mode on a GET request to be JSON, but I'd like the same to happen with a POST. Currently it seems that if there is an error on a POST, it returns XML by default, ignoring the output_mode query string. ignoring the output_mode value in the body..

Here is some quick example code in python..

bsmith@bsmith-laptop!1095 S:0 M:command
-> python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import httplib2
>>> import urllib
>>> 
>>> http_obj = httplib2.Http()
>>> encoded_body = urllib.urlencode({'username':'admin', 'password':'WRONGPASSWORD', 'output_type': 'JSON'})
>>> response = http_obj.request('https://localhost:8089/services/auth/login',
...                             'POST', headers={}, body=encoded_body)
>>> 
>>> print response
({'status': '401', 'content-length': '81', 'server': 'Splunkd', 'connection': 'close', 'date': 'Fri, 30 Jul 2010 16:26:01 GMT', 'content-type': 'text/xml; charset=utf-8'}, '<response>\n<messages>\n<msg type="WARN">Login failed</msg>\n</messages>\n</response>')

Also, I wanted to mention that it would be really cool if you could specify an Accept header as opposed to a query string for output_mode.. It would go like this..

Accept: application/json

or

Accept: text/xml

Tags (2)
1 Solution

goosemo
Explorer

Is that really a post request when you do "?output_type=json"?

But beyond that I don't see an difference in the output when I repeat the request with GET, and I am also not finding any docs on getting json responses back from the auth service?

View solution in original post

0 Karma

goosemo
Explorer

Is that really a post request when you do "?output_type=json"?

But beyond that I don't see an difference in the output when I repeat the request with GET, and I am also not finding any docs on getting json responses back from the auth service?

0 Karma

benjaminws
Explorer

Right, I am assuming the rest of the response (the XML) is the response body.

0 Karma

goosemo
Explorer

The response you are getting is from the simpleRequest method which states:
This method will return a tuple of (serverResponse, serverContent)

    serverResponse: a dict of HTTP status information
    serverContent: the body content

So that first bit isn't even json it seems, but a plain python dict.

0 Karma

benjaminws
Explorer

Thanks for your responses!

IMO, that's kinda silly. Being that it's mentioned that JSON is an acceptable response format, I'd expect all the responses to be in JSON if that's what I ask for. Mixing XML and JSON is fugly and I'm not gonna do it 😞

0 Karma

goosemo
Explorer

Also in looking though the code, the only things the auth seems to expect in the header is username and password.

0 Karma

goosemo
Explorer

Yeah, its all in a few files. The response itself is made by the generateError() in splunk/lib/python2.6/site-packages/splunk/appserver/mrsparkle/controllers/proxy.py

But I don't really know a good way to edit that to make it know when to make json errors. And I don't know if/how that'd break the chain back.

0 Karma

benjaminws
Explorer

Meh, that was left over from a get. Nothing to see there ;).

I would assume in a POST that it would accept headers of some sort..

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 ...