Error creating custom event

Hi Team, I have a similar problem. I keep getting this
“JSON couldn’t be parsed because it’s syntactically invalid”

and below is my simple script. I’m not able to figure out what is wrong here. Appreciate if you can throw some light here.

headers = {"Content-Type":"text/json; charset=utf-8",
  "X-Insert-Key":"*********************************"
}

input_data = [{
  "timestamp": "2022-01-04T11:18:15.6006948Z",
  "eventType": "loginbylebaranoctest",
  "service_": "loginservice",
  "customerId_": "C0PR"
}]

data = {'input_data': input_data}
response = requests.post('https://insights-collector.newrelic.com/v1/accounts/675444/events',headers=headers, data=data)

print(response.text)

Hi, @Rajkumar.Nageswaran: You have data and headers swapped in your call to requests.post(). It should be:

response = requests.post('https://insights-collector.newrelic.com/v1/accounts/675444/events', data=data, headers=headers)
2 Likes

Hi @philweber I have swapped the data and header, still get the same error.

headers = {“Content-Type”:“text/json; charset=utf-8”,
“X-Insert-Key”:"*************************"
}

input_data = [{
“timestamp”: “2022-01-04T11:18:15.6006948Z”,
“eventType”: “loginbylebaranoctest”,
“service_”: “loginservice”,
“customerId_”: “C0PR”
}]
data = {“input_data”: input_data}

response = requests.post(‘https://insights-collector.newrelic.com/v1/accounts/675444/events’, data=data, headers=headers)

print(response.text)

Try this:

import json
import requests

headers = {"Content-Type":"text/json; charset=utf-8",
  "X-Insert-Key":"*************************"
}

content = json.dumps({
  "eventType": "loginbylebaranoctest",
  "service_": "loginservice",
  "customerId_": "C0PR"
})

response = requests.post('https://insights-collector.newrelic.com/v1/accounts/675444/events', data=content, headers=headers)

print(response.text)

Note that timestamp must be within the past 24 hours. I have simply omitted it from the sample code, so the event will have a timestamp set automatically.

1 Like

Hi @philweber This works Thank you. :smiley: let me come back if I have more questions.