Developing for Splunk Enterprise

RESTful api output_mode of POST

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

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

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

Explorer

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

0 Karma

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

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

Explorer

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

0 Karma

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

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
State of Splunk Careers

Access the Splunk Careers Report to see real data that shows how Splunk mastery increases your value and job satisfaction.

Find out what your skills are worth!