Getting Data In

Accessing via C# RESTful

wajihullahbaig
Explorer

Hi
I have been looking around to build an application to using C# RESTful services to gain access to splunk. So far I have not been able to get even the session. Below is the code that I have implemented

private static string BaseUrl = "http://localhost:8000";
        static void Main(string[] args)
        {
            var client = new WebClient();
            var parameters = new Dictionary<string, string>         
                { 
                    { "username", "admin" },
                    { "password", "mypassword" }
                };

            var result = client.UploadString(String.Format("{0}/services/auth/login", BaseUrl),UrlEncode(parameters));
            var doc = XDocument.Load(result);  // load response into XML document (LINQ)
            var key = doc.Elements("sessionKey").Single().Value; // get the one-and-only <sessionKey> element.
            Console.WriteLine("====>sessionkey:  {0}  <====", key);
        }

        // Utility function: 
        private static string UrlEncode(IDictionary<string, string> parameters)
        {
            var sb = new StringBuilder();
            foreach (var val in parameters)
            {
                // add each parameter to the query string, url-encoding the value.
                sb.AppendFormat("{0}={1}&", val.Key, HttpUtility.UrlEncode(val.Value));
            }
            sb.Remove(sb.Length - 1, 1); // remove last '&'
            return sb.ToString();
        }

I keep getting the "404" error continuously on client.UploadString() method. Am I missing something? Do I need to startup some service related to Splunk before I can execute the code above? Splunkd and SplunkWeb are already running as a service. Win7 X64.

Thanks!

Tags (3)
0 Karma
1 Solution

Damien_Dallimor
Ultra Champion

Your Base URL is wrong.

Try :

private static string BaseUrl = "https://localhost:8089";

FYI : Java, Python and Javascript SDK's available here

View solution in original post

chhavi
Explorer
0 Karma

Damien_Dallimor
Ultra Champion

Your Base URL is wrong.

Try :

private static string BaseUrl = "https://localhost:8089";

FYI : Java, Python and Javascript SDK's available here

wcolgate_splunk
Splunk Employee
Splunk Employee
0 Karma

wajihullahbaig
Explorer

OK. Seems to I now have a weak eye sight! But thanks. Working.

0 Karma

wajihullahbaig
Explorer

@Damien- Thanks for the answer. I know the other SDK's. But I have to use C#. Trying out your suggestion for the fix.

0 Karma
Get Updates on the Splunk Community!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...