<?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 HEC not giving JSON output when using python in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/HEC-not-giving-JSON-output-when-using-python/m-p/472655#M81228</link>
    <description>&lt;P&gt;I am fairly new to python and I am trying to use a python script to get the health of my HEC in JSON format.&lt;/P&gt;

&lt;P&gt;When I am using a curl command like below:&lt;BR /&gt;
curl -k -s -u 'username:password' -X GET https:myServername:8088/services/collector/health&lt;BR /&gt;
I get the below response:&lt;BR /&gt;
{"text":"HEC is healthy","code":17}&lt;/P&gt;

&lt;P&gt;But when I am using the same command in python to get json event such as above its giving me an error saying the "NO json objects could be decoded"&lt;BR /&gt;
and when I hash out the json.loads() variable from the below script, the output says in an html format "The request your client sent was too large".&lt;/P&gt;

&lt;P&gt;This might be sending me an html response no matter what. Can you please suggest how to get a JSON response from 8088 port for the /services/collector/health endpoint.&lt;/P&gt;

&lt;P&gt;python script below: &lt;/P&gt;

&lt;H1&gt;!/usr/bin/python&lt;/H1&gt;

&lt;P&gt;import json&lt;BR /&gt;
import os&lt;BR /&gt;
import re&lt;BR /&gt;
import sys&lt;BR /&gt;
import urllib&lt;BR /&gt;
import httplib2&lt;BR /&gt;
import credentials&lt;BR /&gt;
import requests&lt;/P&gt;

&lt;P&gt;username = credentials.username&lt;BR /&gt;
baseurl = credentials.baseurl&lt;BR /&gt;
password = credentials.password&lt;BR /&gt;
hecBaseUrl = '&lt;A href="https://myServer:8088" target="_blank"&gt;https://myServer:8088&lt;/A&gt;'&lt;BR /&gt;
myhttp = httplib2.Http(disable_ssl_certificate_validation=True)&lt;/P&gt;

&lt;P&gt;try:&lt;/P&gt;

&lt;P&gt;cmdurl = '/services/auth/login'&lt;BR /&gt;
  serverResponse = myhttp.request(baseurl + cmdurl, 'POST', headers={}, body=urllib.urlencode({'username':username, 'password':password,'output_mode':'json'}))[1]&lt;/P&gt;

&lt;H1&gt;print serverResponse&lt;/H1&gt;

&lt;P&gt;parsed_json = json.loads(serverResponse)&lt;BR /&gt;
  sessionKey = parsed_json['sessionKey']&lt;BR /&gt;
  print "sessionKey is %s" % sessionKey&lt;BR /&gt;
  hecUrl = '/services/collector/health'&lt;BR /&gt;
  totalUrl = (hecBaseUrl + hecUrl)&lt;BR /&gt;
  print totalUrl&lt;BR /&gt;
  hecServerResponse = myhttp.request(hecBaseUrl + hecUrl, 'GET', headers={}, body=urllib.urlencode({'output_mode':'json'}))[1]&lt;BR /&gt;
  parsed_json_hec = json.loads(hecServerResponse)&lt;BR /&gt;
  print parsed_json_hec&lt;BR /&gt;
  print hecServerResponse&lt;BR /&gt;
except Exception, err:&lt;BR /&gt;
  sys.stderr.write('Error: %s\n' %str(err))&lt;/P&gt;</description>
    <pubDate>Wed, 30 Sep 2020 04:12:42 GMT</pubDate>
    <dc:creator>rohitmaheshwari</dc:creator>
    <dc:date>2020-09-30T04:12:42Z</dc:date>
    <item>
      <title>HEC not giving JSON output when using python</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/HEC-not-giving-JSON-output-when-using-python/m-p/472655#M81228</link>
      <description>&lt;P&gt;I am fairly new to python and I am trying to use a python script to get the health of my HEC in JSON format.&lt;/P&gt;

&lt;P&gt;When I am using a curl command like below:&lt;BR /&gt;
curl -k -s -u 'username:password' -X GET https:myServername:8088/services/collector/health&lt;BR /&gt;
I get the below response:&lt;BR /&gt;
{"text":"HEC is healthy","code":17}&lt;/P&gt;

&lt;P&gt;But when I am using the same command in python to get json event such as above its giving me an error saying the "NO json objects could be decoded"&lt;BR /&gt;
and when I hash out the json.loads() variable from the below script, the output says in an html format "The request your client sent was too large".&lt;/P&gt;

&lt;P&gt;This might be sending me an html response no matter what. Can you please suggest how to get a JSON response from 8088 port for the /services/collector/health endpoint.&lt;/P&gt;

&lt;P&gt;python script below: &lt;/P&gt;

&lt;H1&gt;!/usr/bin/python&lt;/H1&gt;

&lt;P&gt;import json&lt;BR /&gt;
import os&lt;BR /&gt;
import re&lt;BR /&gt;
import sys&lt;BR /&gt;
import urllib&lt;BR /&gt;
import httplib2&lt;BR /&gt;
import credentials&lt;BR /&gt;
import requests&lt;/P&gt;

&lt;P&gt;username = credentials.username&lt;BR /&gt;
baseurl = credentials.baseurl&lt;BR /&gt;
password = credentials.password&lt;BR /&gt;
hecBaseUrl = '&lt;A href="https://myServer:8088" target="_blank"&gt;https://myServer:8088&lt;/A&gt;'&lt;BR /&gt;
myhttp = httplib2.Http(disable_ssl_certificate_validation=True)&lt;/P&gt;

&lt;P&gt;try:&lt;/P&gt;

&lt;P&gt;cmdurl = '/services/auth/login'&lt;BR /&gt;
  serverResponse = myhttp.request(baseurl + cmdurl, 'POST', headers={}, body=urllib.urlencode({'username':username, 'password':password,'output_mode':'json'}))[1]&lt;/P&gt;

&lt;H1&gt;print serverResponse&lt;/H1&gt;

&lt;P&gt;parsed_json = json.loads(serverResponse)&lt;BR /&gt;
  sessionKey = parsed_json['sessionKey']&lt;BR /&gt;
  print "sessionKey is %s" % sessionKey&lt;BR /&gt;
  hecUrl = '/services/collector/health'&lt;BR /&gt;
  totalUrl = (hecBaseUrl + hecUrl)&lt;BR /&gt;
  print totalUrl&lt;BR /&gt;
  hecServerResponse = myhttp.request(hecBaseUrl + hecUrl, 'GET', headers={}, body=urllib.urlencode({'output_mode':'json'}))[1]&lt;BR /&gt;
  parsed_json_hec = json.loads(hecServerResponse)&lt;BR /&gt;
  print parsed_json_hec&lt;BR /&gt;
  print hecServerResponse&lt;BR /&gt;
except Exception, err:&lt;BR /&gt;
  sys.stderr.write('Error: %s\n' %str(err))&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 04:12:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/HEC-not-giving-JSON-output-when-using-python/m-p/472655#M81228</guid>
      <dc:creator>rohitmaheshwari</dc:creator>
      <dc:date>2020-09-30T04:12:42Z</dc:date>
    </item>
    <item>
      <title>Re: HEC not giving JSON output when using python</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/HEC-not-giving-JSON-output-when-using-python/m-p/472656#M81229</link>
      <description>&lt;P&gt;Your code is unnecessarily complex&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import requests
from requests.auth import HTTPBasicAuth
import json

username = 'username'
password = 'password'
hecBaseUrl = 'https://myServer:8088'

response = json.loads(requests.get('{}/services/collector/health'.format(hecBaseUrl), auth=HTTPBasicAuth(username, password), verify=False).text)

print(json.dumps(response, indent=4))
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Feb 2020 20:39:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/HEC-not-giving-JSON-output-when-using-python/m-p/472656#M81229</guid>
      <dc:creator>arjunpkishore5</dc:creator>
      <dc:date>2020-02-14T20:39:10Z</dc:date>
    </item>
    <item>
      <title>Re: HEC not giving JSON output when using python</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/HEC-not-giving-JSON-output-when-using-python/m-p/472657#M81230</link>
      <description>&lt;P&gt;Thankyou Arjun,&lt;/P&gt;

&lt;P&gt;This works.&lt;BR /&gt;
I had to add a zero in the curly braces.&lt;BR /&gt;
response = json.loads(requests.get('{0}/services/collector/health'.format(hecBaseUrl), auth=HTTPBasicAuth(username, password), verify=False).text)&lt;/P&gt;

&lt;P&gt;If possible can you please explain this request.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Feb 2020 15:24:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/HEC-not-giving-JSON-output-when-using-python/m-p/472657#M81230</guid>
      <dc:creator>rohitmaheshwari</dc:creator>
      <dc:date>2020-02-18T15:24:12Z</dc:date>
    </item>
  </channel>
</rss>

