简体   繁体   English

Tableau REST API 使用 Axios 节点发布

[英]Tableau REST API Publishing with Axios Node

I am trying to publish a workbook to the tableau REST API with Node and Axios.我正在尝试使用 Node 和 Axios 将工作簿发布到表格 REST API。 I have had success in the past using Python to publish to the API so I am familiar with the API in general.过去我曾成功使用 Python 发布到 API 所以我一般熟悉 API。

The docs provided by Tableau give an example publishing with AJAX: Tableau 提供的文档给出了使用 AJAX 发布的示例:

//Publish Workbook
            function publishWorkbook() {
              var form = new FormData();
              form.append("request_payload", "<tsRequest><workbook name=\"restWorkbook\" ><project id=\"06ca4b01-f882-4f7a-b4b5-60eb8e8bff8f\" ></project></workbook></tsRequest>");
              form.append("tableau_workbook", "PATH TO Workbook");
              var settings = {
                "async": true,
                "crossDomain": true,
                "url": url + "sites/" + siteid + "/workbooks?overwrite=true",
                "method": "POST",
                "headers": {
                  "x-tableau-auth": auth
                },
                "processData": false,
                "contentType": false,
                "mimeType": "multipart/mixed",
                "data": form
              }
              $.ajax(settings).done(function(response) {
                console.log(response);
              });
            }

This is my attempt to do the same thing with Axios ( https://masteringjs.io/tutorials/axios/form-data ):这是我尝试用 Axios ( https://masteringjs.io/tutorials/axios/form-data ) 做同样的事情:

function publishWorkbook(base_url, site_id, project_id, workbook_filepath, auth_token) {
  const url = base_url + `sites/${site_id}/workbooks?overwrite=true`;
  const formData = new FormData();
  formData.append("request_payload", `<tsRequest><workbook name="test" ><project id="${project_id}"></project></workbook></tsRequest>`);
  formData.append("data", fs.createReadStream(workbook_filepath));
  const headers = formData.getHeaders();
  headers["x-tableau-auth"] =  auth_token;
  return axios.post(url, formData, {headers:headers});
}

I am receiving a 406 error code.我收到 406 错误代码。 Inspecting the FormData shows me a dataSize: 0, which seems weird.检查 FormData 会显示一个dataSize: 0,这看起来很奇怪。 Any thoughts?有什么想法吗? Thanks!谢谢!

This is how I got it working in the end:这就是我最终让它工作的方式:

publish = (project_id,workbook_path) => {
    return fsp.readFile(workbook_path).then(data => {
        const url = this.#site_url + "workbooks?workbookType=twbx&overwrite=true";
        var form = new FormData();
        const xml_payload = `<tsRequest><workbook name=\"restWorkbook\" ><project id=\"${project_id}\" ></project></workbook></tsRequest>`;
        form.append("request_payload", xml_payload, {
            filename: "",
            contentType: 'text/xml'
        });
        form.append("tableau_workbook", data, {
            filename: path.basename(workbook_path),
            contentType: 'application/octet-stream'
        });
        const form_headers = form.getHeaders()
        form_headers['content-type'] = form_headers['content-type'].replace('form-data','mixed')
        const config = {
            headers: Object.assign({}, this.#auth_header, form_headers)
        };
        return axios.post(url, form, config);
    }).catch(error => {
        console.log("publishing failed")
        if('response' in error && 'status' in error['response']){
            console.log(error['response']['status'],error['response']['statusText'])
            console.log(error['response']['data'])
        }else{
            console.log(error)
        }
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM