Dashboards & Visualizations

How do I send data to HTTP Event Collector via Javascript with CORS

dperre_splunk
Splunk Employee
Splunk Employee

Can anyone share some sample javascript code to send data over javascript to Splunk to avoid CORS (Cross Origin Resource Sharing). I am thinking of embedding this code in an app.

0 Karma

dperre_splunk
Splunk Employee
Splunk Employee

To avoid anyone experiencing any pain in trying to solve this problem here is what you need to do.

Under server.conf add the following lines

[httpServer] 
crossOriginSharingPolicy = *

Under web.conf add the following lines

[settings]
crossOriginSharingPolicy = *

Under inputs.conf add the following lines

[http]
crossOriginSharingPolicy = *

Then in javascript this will work

var xhttp = new XMLHttpRequest();
                xhttp.open("POST", "https://yourservername.local/services/collector", true);
                xhttp.setRequestHeader("Authorization", "Splunk YOURHECCODE");
                xhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xhttp.send(JSON.stringify({
                  "event": "THE STRING YOU WANT TO SEND"
                  }));

Now these settings will work but you should think about crossOriginSharingPolicy before allowing everything. The docs helped me in this one.

Also, if you want to test this at home with a self signed certificate you can run ngrok to help you out with the following code

./ngrok http https://127.0.0.1:8088
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...