admin管理员组文章数量:1302272
NOTE: when I first posted this question... I had no idea even where to begin or even how to give the question a good title. After several ments, I added more data to the question and then eventually changed the question title to what it is now - thanks for the help everyone !
I am using Nodejs in a server application. using Visual Code for my debugger.
I am reading a Garmin Fit File off disk using this code:
var fName = path.join(__dirname, './4861335825.fit');
fs.readFile(fName, function (err, content) {
'Content' contains the following:
I am downloading the SAME Garmin Fit File via a REST call. Here is what the response looks like from the REST call
Here is the response header:
I am trying to get the response.data to be the same as when I read it off disk. Here is the code where I try to create a buffer to read the data
encoding = 'utf8'
const buf1 = Buffer.from(response.data, encoding);
Per Pointy's ment, I have tried using the following encoding for the REST call BOTH fail
encoding = 'utf8';
encoding = 'utf16le';
Here is what the buffer looks like:
The files are the SAME, the difference is one read off disk and the other is trying to be read from the response.data from the REST call that downloads it.
The data appears to be the same in the two buffers:
[14, 16, 77, ...]
However, the type and sizes are radically different:
I should also state, the file that is read from disk successfully parses using the fit-file-parser. The one from the REST call fails to parse.
Here is the parsing code I am using this parser:
// Create a FitParser instance (options argument is optional)
var fitParser = new FitParser({
force: true,
speedUnit: 'km/h',
lengthUnit: 'km',
temperatureUnit: 'kelvin',
elapsedRecordField: true,
mode: 'cascade',
});
var error = null;
var data = null;
try{
fitParser.parse(responseData, function (error, data){
if(error){
console.log(error);
return null;
}
return data;
});
}
catch(err){
console.log('error',err);
}
How can I make the buffer from the REST download the same as the one that is read off disk?
NOTE: when I first posted this question... I had no idea even where to begin or even how to give the question a good title. After several ments, I added more data to the question and then eventually changed the question title to what it is now - thanks for the help everyone !
I am using Nodejs in a server application. using Visual Code for my debugger.
I am reading a Garmin Fit File off disk using this code:
var fName = path.join(__dirname, './4861335825.fit');
fs.readFile(fName, function (err, content) {
'Content' contains the following:
I am downloading the SAME Garmin Fit File via a REST call. Here is what the response looks like from the REST call
Here is the response header:
I am trying to get the response.data to be the same as when I read it off disk. Here is the code where I try to create a buffer to read the data
encoding = 'utf8'
const buf1 = Buffer.from(response.data, encoding);
Per Pointy's ment, I have tried using the following encoding for the REST call BOTH fail
encoding = 'utf8';
encoding = 'utf16le';
Here is what the buffer looks like:
The files are the SAME, the difference is one read off disk and the other is trying to be read from the response.data from the REST call that downloads it.
The data appears to be the same in the two buffers:
[14, 16, 77, ...]
However, the type and sizes are radically different:
I should also state, the file that is read from disk successfully parses using the fit-file-parser. The one from the REST call fails to parse.
Here is the parsing code I am using this parser: https://www.npmjs./package/fit-file-parser
// Create a FitParser instance (options argument is optional)
var fitParser = new FitParser({
force: true,
speedUnit: 'km/h',
lengthUnit: 'km',
temperatureUnit: 'kelvin',
elapsedRecordField: true,
mode: 'cascade',
});
var error = null;
var data = null;
try{
fitParser.parse(responseData, function (error, data){
if(error){
console.log(error);
return null;
}
return data;
});
}
catch(err){
console.log('error',err);
}
How can I make the buffer from the REST download the same as the one that is read off disk?
Share edited May 1, 2020 at 14:33 MLissCetrus asked May 1, 2020 at 13:13 MLissCetrusMLissCetrus 4735 silver badges23 bronze badges 14-
Your Buffer you decided to use encoding, but for you readFile you never, maybe try using
utf8
for you readFile too. – Keith Commented May 1, 2020 at 13:17 - stackoverflow./users/6870228/keith when I parse the file off disk it succeeds. when I parse the file from REST call it fails. – MLissCetrus Commented May 1, 2020 at 13:22
- 1 You're telling the client-side code to interpret the content as a UTF-8 encoded character stream. Is it? edit — wait, is that HTTP request being made from Node? I guess so. – Pointy Commented May 1, 2020 at 13:24
- stackoverflow./users/182668/pointy - update question for types of encoding used. ( this is all backend code ) – MLissCetrus Commented May 1, 2020 at 13:28
- What are the response headers? – josh.trow Commented May 1, 2020 at 13:29
1 Answer
Reset to default 8Thanks to @josh.trow for giving me a suggestion.
Here is what fixed this:
before:
var config = {
headers:{Authorization:header}
}
after:
var config = {
responseType: 'arraybuffer',
headers:{Authorization:header}
}
And the axios call returns the same buffer as the file read does !
本文标签: javascriptHow can I make axios return the correct type of bufferStack Overflow
版权声明:本文标题:javascript - How can I make axios return the correct type of buffer? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741687641a2392536.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论