最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I make axios return the correct type of buffer? - Stack Overflow

programmeradmin2浏览0评论

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
 |  Show 9 more ments

1 Answer 1

Reset to default 8

Thanks 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 !

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>