Splunk Enterprise

Why are we getting Error "JSON in the request is invalid" while updating KV lookup from python code?

Siddharth
Path Finder

This is the code 


import requests
import datetime


now = datetime.datetime.now()

# print(now)
data = {'ticket_id':'CH-12345','response_code':200,'service':'Ec2','problem_type':'server_down','time':now}

headers = {
'Content-Type': 'application/json'
}
response = requests.post('https://localhost:8089/servicesNS/nobody/TA-cherwell-data-pull/storage/collections/data/cherwell_dat...' ,headers=headers ,data=data, verify=False , auth=('admin' , 'changeme'))


print(response.text) 

This is the error I am getting 

<msg type="ERROR">JSON in the request is invalid. (
JSON parse error at offset 1 of file "ticket_id=CH-12345&amp;response_code=200&amp;service=Ec2&amp;problem_type=server_down&amp;time=2022-08-31+20%3A28%3A53.237962": Unexpected character while parsing literal token: 'i'
)</msg>


Please let me know if you need any more help

Labels (1)
0 Karma
1 Solution

Siddharth
Path Finder

Got the solution 

This error was happening because of not sending data in JSON format  


just added this code 

data1=json.dumps(data)

and passed data1 instead of data and now it is working perfectly fine  

import requests
import datetime
import json


now = datetime.datetime.now()

# print(now)
data = {'ticket_id':'CH-12345','response_code':200,'service':'Ec2','problem_type':'server_down','time':str(now)}

 

headers = {
'Content-Type': 'application/json'
}
data1=json.dumps(data)
response = requests.post('https://localhost:8089/servicesNS/nobody/TA-cherwell-data-pull/storage/collections/data/cherwell_dat...' ,headers=headers, data=data1 , verify=False , auth=('admin' , 'changeme'))
print(response.status_code)
print(response.text)

View solution in original post

0 Karma

Siddharth
Path Finder

Got the solution 

This error was happening because of not sending data in JSON format  


just added this code 

data1=json.dumps(data)

and passed data1 instead of data and now it is working perfectly fine  

import requests
import datetime
import json


now = datetime.datetime.now()

# print(now)
data = {'ticket_id':'CH-12345','response_code':200,'service':'Ec2','problem_type':'server_down','time':str(now)}

 

headers = {
'Content-Type': 'application/json'
}
data1=json.dumps(data)
response = requests.post('https://localhost:8089/servicesNS/nobody/TA-cherwell-data-pull/storage/collections/data/cherwell_dat...' ,headers=headers, data=data1 , verify=False , auth=('admin' , 'changeme'))
print(response.status_code)
print(response.text)

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

Great @Siddharth 

Please accept your answer to close this question.

KV

Happy Splunking

 

0 Karma
Get Updates on the Splunk Community!

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

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

Combine Multiline Logs into a Single Event with SOCK: a Step-by-Step Guide for ...

Combine multiline logs into a single event with SOCK - a step-by-step guide for newbies Olga Malita The ...