Dashboards & Visualizations

Can not access REST API by xmlhttprequest call in javascript

Path Finder

Hi Splunkers,

Currently I want to change ownership of lookup table and its working fine with curl command
curl -k -u usr:pwd https://localhost:8089/servicesNS/<new owner>/app_name/data/lookup-table-files/lookupfile.csv/acl -d owner=<new owner> -d sharing=user
This I am implementing in javascript by Ajax and jquery Ajax. But are giving me result 404 page not found error.
By XMLHttpRequest:

if(window.XMLHttpRequest){
var xmlhttp = new XMLHttpRequest();
}else if(window.ActiveXObject) {
var xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
var record = {
"owner": form_uid,
"sharing": "user"
};
var url = '/servicesNS/'+form_uid+'/app_name/data/lookup-table-files/lookup_'+form_uid+'.csv/acl';
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText);
}
};
xmlhttp.send(JSON.stringify(record));

By Jquery Ajax:

$.ajax({
type: "POST",
url:'/servicesNS/'+form_uid+'/app_name/data/lookup-table-files/lookup_'+form_uid+'.csv/acl',
dataType: 'text/csv',
data: JSON.stringify(record),
beforeSend: function (xmlhttp) {
xmlhttp.setRequestHeader('Accept-Language', 'en_US');
}
});

Both ways are giving 404 page not found error. Can any one help me out. Whats wrong in this?

0 Karma
1 Solution

I think your JavaScript will be a part of any of your Splunk screen/view which is serving in 8000 port... When you are trying to access a relative path in ajax (like this var url = '/servicesNS/'+formuid+'/appname/data/lookup-table-files/lookup'+formuid+'.csv/acl';) it will make call to
"http(s)://localhost:8000/servicesNS/'+formuid+'/appname/data/lookup-table-files/lookup'+formuid+'.csv/acl';"

And obviously that end point is not available in splunk web running on 8000 port...

That is why you are getting 404 error here

Try things in this link... This is what you want..

http://dev.splunk.com/javascript

http://dev.splunk.com/view/javascript-sdk/SP-CAAAEC9

View solution in original post

0 Karma

I think your JavaScript will be a part of any of your Splunk screen/view which is serving in 8000 port... When you are trying to access a relative path in ajax (like this var url = '/servicesNS/'+formuid+'/appname/data/lookup-table-files/lookup'+formuid+'.csv/acl';) it will make call to
"http(s)://localhost:8000/servicesNS/'+formuid+'/appname/data/lookup-table-files/lookup'+formuid+'.csv/acl';"

And obviously that end point is not available in splunk web running on 8000 port...

That is why you are getting 404 error here

Try things in this link... This is what you want..

http://dev.splunk.com/javascript

http://dev.splunk.com/view/javascript-sdk/SP-CAAAEC9

View solution in original post

0 Karma

Path Finder

Ya.. I changed API call to https://localhost:8089/servicesNS/'+form_uid+'/app_name/data/lookup-table-files/lookup_'+form_uid+'.... Its working fine.
Currently I am getting net::ERR_CONNECTION_REFUSED error.
I checked internet connection and removed proxy setting. Still error is there.

0 Karma

SplunkTrust
SplunkTrust

That's exactly what I suggested yesterday but you accepted this answer instead 😞

0 Karma

Path Finder

Ya. Yest I had some other issue of header setting and call.
Even after changing https://localhost:8089 It did worked.

Thank u so much sir. Now I am trying accept your answer its not there.
As both answer are correct (yours and paramagurukarthikeyan )
How to accept both the answer?

0 Karma

SplunkTrust
SplunkTrust

You can only accept one and typically you'd accept the first one that's correct. Is what it is...

0 Karma

Path Finder

OK.. Thank u for the information.
As it is reply to comment of someone else post. There is no accept for reply section of your answer which is correct.
I have 1 question:
for any management port 8089 REST API access require SSL certification mandatory ?

0 Karma

SplunkTrust
SplunkTrust

What was the fix?

0 Karma

Path Finder

As I was calling REST API from url = '/servicesNS/'+form_uid+'/app_name/data/lookup-table-files/lookup_'+form_uid+'.csv/acl' Assuming that it will append prefix its part.
But, it was pointing to web server port 8000
'http(s)://localhost:8000/servicesNS/'+form_uid+'/app_name/data/lookup-table-files/lookup_'+form_uid+'.csv/acl'
I changed to management port 8089
https://localhost:8089/servicesNS/'+form_uid+'/app_name/data/lookup-table-files/lookup_'+form_uid+'....
It worked fine.
Thank u..

0 Karma

Ultra Champion
0 Karma

Path Finder

Hi Damien Dallimore,

         setting up CORS policy comes only when app outside the splunk web server. I just want to execute this below curl command in javascript. I have tried with XMLHttpRequest and Jquery ajax.

Both are giving me 404 page not found error.
It works fine with curl command.

curl -k -u usr:pwd https://localhost:8089/servicesNS/<new owner>/app_name/data/lookup-table-files/lookupfile.csv/acl -d owner=<new owner> -d sharing=user

Whats going wrong here?

0 Karma

SplunkTrust
SplunkTrust

In the curl command youre specifying an existing lookup name "lookupfile.csv". In the other you're appending the form UID to the lookup name but then that lookup name shouldn't exist unless you already created it.

0 Karma

Path Finder

Before calling this function, I have already created lookup file in that location. Its present above given location, I have verified it.
Problem here is why this Ajax call for this API giving me 404 error.
How to resolve or how to write REST API call from javascript?

0 Karma

SplunkTrust
SplunkTrust

Something is wrong with the URL. 404 not found means the resource isn't there on the web server. Have you tried adding https://localhost:8089 to the url?

0 Karma