0

I am a complete beginner with REST API and I could not figure out how I am to proceed. I installed Postman and was successfully able to get the Token, but I am not sure how to send the raw XML payload in javascript.

 <tsRequest>
      <credentials name ="XXX" password="YYY"  >
           <site contenturl = "" />
      </credentials>
 </tsRequest>

I have :

 httpRequest.open('POST', 'http://MY-SERVER/api/2.4/auth/signin', false);
 httpRequest.setRequestHeader("Content-type", "application/xml");

Not sure how to add the xml payload. I have access to a Tableau Server(MY-SERVER) and everything. Any help would be greatly appreciated!

Thank you!

2 Answers2

0

You are getting closer, you just need to use the send method to send your XML: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send

Just make sure that your XML is properly encoded in javascript when you're inputting it. So if you are using double quotes inside your XML, make sure you have single quotes to declare your string in javascript (e.g.) var data = '<credentials name="XXX" >';

Related: Send POST data using XMLHttpRequest

AnilRedshift
  • 7,937
  • 7
  • 35
  • 59
0

In addition to @AnilRedshift answer, here's the functioning code:

login_details=[];
function getToken() {

var url = "http://yourServerAddress/api/2.0/auth/signin";
var params = "<tsRequest><credentials name='Username' password='UserPassword' ><site contentUrl='' /></credentials></tsRequest>";

    return zuo = new Promise(function(resolve,reject){

            var xhr = new XMLHttpRequest(); 
            xhr.open("POST", url, true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.withCredentials = true;
            xhr.onload= function(){ 
                if (this.status === 200) {
                    var parsed_xml = JSON.parse(JSON.stringify(x2js.xml_str2json(xhr.responseText)))
                    login_details.push(parsed_xml.tsResponse.credentials._token);                   login_details.push(parsed_xml.tsResponse.credentials.site._id);
                    resolve(login_details);
                }
            }
            xhr.onerror=reject;
        xhr.send();

        })


}

function getWorkbooks(){

var url = "http://serveraddress//api/2.3/sites/"+login_details[1]+"/workbooks?pageSize=1000";

    return zuo = new Promise(function(resolve,reject){

            var xhr = new XMLHttpRequest(); 
            xhr.open("GET", url, true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.setRequestHeader("X-Tableau-Auth",login_details[0]);
            xhr.onload= function(){ 
                if (this.status === 200) {

                    var workbooks = JSON.parse(JSON.stringify(x2js.xml_str2json(xhr.responseText)))

                    for (var f=0;f<workbooks.tsResponse.workbooks.workbook.length;f++){
                        if(workbooks.tsResponse.workbooks.workbook[f].project._name=="Default"){
                            workbooks_list.push(workbooks.tsResponse.workbooks.workbook[f]._id)
                        }
                       resolve();
                    }

                }
            }
            xhr.onerror= function(){ 


                    console.log(xhr.responseText);              
            }

        xhr.send();

        })


}

Invoke the code with:

getToken()
    .then(function(login_details){
        console.log(login_details[0]+"/"+login_details[1]);
    })
    .then(function(){
        getWorkbooks();
    })

getToken() function gets the login token which has to be used in all subsequent calls. getWorkbooks() fetches all dashboards in 'Default' project but this kind of request can be used for all GET type requests.

Please note that this approach uses hardcoded values for password and username which is generally not the best practice. It would be way better to use server side scripting or encrypting (better but still with flavs).

You can find whole step by step tutorial and running code here: http://meowbi.com/2017/10/23/tableau-fields-definition-undocumented-api/

darth0s
  • 165
  • 2
  • 13