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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...