gpt4 book ai didi

javascript - 如何从返回 CSV 文件的 API 获取数据

转载 作者:行者123 更新时间:2023-12-03 06:51:34 27 4
gpt4 key购买 nike

我正在使用 Pluralsight API,他们的 API 将 CSV 文件作为结果进行响应,我想从响应中获取这些数据并打印出来。

这是 Pluralsight API 的 URL: https://app.pluralsight.com/plans/api/reports/docs

这是我正在尝试执行的代码:

  request
.get('https://api.pluralsight.com/api-v0.9/users?planId=x&token=y')
.on('response', function (response) {
res.json(response);
})

除了以下数据之外,响应不显示任何数据:

  {
"statusCode": 200,
"headers": {
"cache-control": "private",
"content-disposition": "attachment; filename=Users.csv",
"content-type": "text/csv",
"date": "Fri, 27 May 2016 03:42:06 GMT",
"ps-build": "2016.5.1849.0",
"ps-node": "0Q5JR",
"ps-responsetime": "00:00:00.1406230",
"content-length": "11391",
"connection": "Close"
},
"request": {
"uri": {
"protocol": "https:",
"slashes": true,
"auth": null,
"host": "api.pluralsight.com",
"port": 443,
"hostname": "api.pluralsight.com",
"hash": null,
"search": "?planId=x&token=y",
"query": "planId=x&token=y",
"pathname": "/api-v0.9/users",
"path": "/api-v0.9/users?planId=x&token=y",
"href": "https://api.pluralsight.com/api-v0.9/users?planId=x&token=y",
},
"method": "GET",
"headers": {}
}
}

无论如何,我可以从附件中获取文件并将其解压吗?

谢谢

最佳答案

请求正文可通过 dataend 事件访问。

var body = [];
request
.get('https://api.pluralsight.com/api-v0.9/users?planId=x&token=y')
.on('data', function (chunk) {
body.push(chunk);
})
.on('end', function () {
body = body.join('');
// body now contains csv contents as a string
});

关于javascript - 如何从返回 CSV 文件的 API 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37474486/

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