gpt4 book ai didi

javascript - 使用 Axios Node 发布 Tableau REST API

转载 作者:行者123 更新时间:2023-12-05 06:56:34 24 4
gpt4 key购买 nike

我正在尝试使用 Node 和 Axios 将工作簿发布到 tableau REST API。我过去曾成功使用 Python 发布到 API,因此我总体上熟悉 API。

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);
});
}

这是我尝试用 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});
}

我收到 406 错误代码。检查 FormData 显示了一个 dataSize: 0,,这看起来很奇怪。有什么想法吗?谢谢!

最佳答案

这就是我最终实现它的方式:

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)
}
})
}

关于javascript - 使用 Axios Node 发布 Tableau REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65098909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com