Splunk Dev

How to fix the error: Splunk HEC too many values to unpack?

Juan_Leon
Explorer

Hello, I am using python to test sending events to splunk via hec, but I am getting the following error. this error is more of a python issue than splunk,  I was wondering if any one had similar issue or have a suggestion.   Thank you.

 

user_data_str looks like below
{
    "GivenName": "MyName",
    "sn": "MyLastN",
    "UserPrincipalName": "MyName@client.com",
    "sAMAccountName": "mysama",
    "enabled": "[]",
    "co": "United States",
    "sourcetype": "my_hec_test",
    "time": 9999999999.8921528
}

Header looks like below

{
    'Authorization': 'Splunk abcdefgh-1234-ijkl-5678-mnopqrstu123456'
}

 

 

user_data_str include 3 users similar to above example

 

>>> print(range(len(user_data_str)))
range(0, 3)
>>> type(user_data_str)
<class 'list'>
>>> type(header)
<class 'dict'>
>>>
>>> requests.post('https://hec.splunkcloud.com/services/collector/event', headers = header, data = user_data_str, verify=False)
Traceback (most recent call last):
  ...
  ...
  ...
  ...
ValueError: too many values to unpack (expected 2)

 

 

Labels (1)
0 Karma
1 Solution

yeahnah
Motivator

Hi @Juan_Leon 

Try it this way...

 

 

 

>>> import requests
>>> import json
>>> url='https://splunk-hec-url:8088/services/collector/event'
>>> authHeader = {'Authorization': 'Splunk my_token_value'}
>>> json_data = '''{
	"sourcetype": "_json",
	"event": [{
			"GivenName": "MyName",
			"sn": "MyLastN",
			"UserPrincipalName": "MyName@client.com",
			"sAMAccountName": "mysama",
			"enabled": "[]",
			"co": "United States",
			"time": 9999999999.8921528
		},
		{
			"GivenName": "MyName",
			"sn": "MyLastN",
			"UserPrincipalName": "MyName@client.com",
			"sAMAccountName": "mysama",
			"enabled": "[]",
			"co": "Canada",
			"time": 9999999999.9999999
		}
	]
}'''
>>> data = json.loads(json_data)
>>> type(data)
<class 'dict'>
>>> r = requests.post(url, headers=authHeader, json=data, verify=False)
>>> print(r.text)
{"text":"Success","code":0}
>>>
>>> event = '''
{
  "sourcetype": "my_hec_test",
  "event":{
		"GivenName": "MyName",
		"sn": "MyLastN",
		"UserPrincipalName": "MyName@client.com",
		"sAMAccountName": "mysama",
		"enabled": "[]",
		"co": "United States"
	}
}
{
  "sourcetype": "my_hec_test",
  "event":{
			"GivenName": "MyName",
			"sn": "MyLastN",
			"UserPrincipalName": "MyName@client.com",
			"sAMAccountName": "mysama",
			"enabled": "[]",
			"co": "Canada"
		}
}
'''
>>> r = requests.post(url, headers=authHeader, data=event, verify=False)
>>> print(r.text)
{"text":"Success","code":0}

 

 

 

Hope it helps

View solution in original post

yeahnah
Motivator

Hi @Juan_Leon 

Try it this way...

 

 

 

>>> import requests
>>> import json
>>> url='https://splunk-hec-url:8088/services/collector/event'
>>> authHeader = {'Authorization': 'Splunk my_token_value'}
>>> json_data = '''{
	"sourcetype": "_json",
	"event": [{
			"GivenName": "MyName",
			"sn": "MyLastN",
			"UserPrincipalName": "MyName@client.com",
			"sAMAccountName": "mysama",
			"enabled": "[]",
			"co": "United States",
			"time": 9999999999.8921528
		},
		{
			"GivenName": "MyName",
			"sn": "MyLastN",
			"UserPrincipalName": "MyName@client.com",
			"sAMAccountName": "mysama",
			"enabled": "[]",
			"co": "Canada",
			"time": 9999999999.9999999
		}
	]
}'''
>>> data = json.loads(json_data)
>>> type(data)
<class 'dict'>
>>> r = requests.post(url, headers=authHeader, json=data, verify=False)
>>> print(r.text)
{"text":"Success","code":0}
>>>
>>> event = '''
{
  "sourcetype": "my_hec_test",
  "event":{
		"GivenName": "MyName",
		"sn": "MyLastN",
		"UserPrincipalName": "MyName@client.com",
		"sAMAccountName": "mysama",
		"enabled": "[]",
		"co": "United States"
	}
}
{
  "sourcetype": "my_hec_test",
  "event":{
			"GivenName": "MyName",
			"sn": "MyLastN",
			"UserPrincipalName": "MyName@client.com",
			"sAMAccountName": "mysama",
			"enabled": "[]",
			"co": "Canada"
		}
}
'''
>>> r = requests.post(url, headers=authHeader, data=event, verify=False)
>>> print(r.text)
{"text":"Success","code":0}

 

 

 

Hope it helps

Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Observe and Secure All Apps with Splunk

 Join Us for Our Next Tech Talk: Observe and Secure All Apps with SplunkAs organizations continue to innovate ...

What's New in Splunk Observability - August 2025

What's New We are excited to announce the latest enhancements to Splunk Observability Cloud as well as what is ...

Introduction to Splunk AI

How are you using AI in Splunk? Whether you see AI as a threat or opportunity, AI is here to stay. Lucky for ...