【问题标题】:How to save octet stream received as REST response to the file system?如何将收到的八位字节流作为 REST 响应保存到文件系统?
【发布时间】:2018-09-12 08:15:47
【问题描述】:

在尝试保存使用Unirest client 接收的octet stream JSON 格式响应(包含专有格式的文件数据)时,格式已损坏,用于打开该格式的(专有)软件无法打开此保存的文件.收到Data encoding mismatch 错误。

当 REST 调用通过基于 nodejs 的 POSTMAN chrome 应用程序驱动时,同样的工作正常

任何线索或线索。

以下是nodejs中用于接收八位字节流的代码:

urClient.get(url)
    .header('Authorization', '<token>')
    .header('Content-Type', 'application/json')
    .end(
        function(response) {
            log.info('+++++++++++++++'+response.code);
            //log.info('+++++++++++++++'+response);
            //log.info('+++++++++++++++'+response.body);
            //log.info('+++++++++++++++'+response.keys);
            fs.writeFile(`${destination}\\${fileName}.${fileExtension}`, response.data, function (error) {
                if (error) {
                    response = {
                        "error": error
                    };
                    res.statusCode = 400;
                    res.json(response); }
            });
        });

【问题讨论】:

    标签: javascript node.js postman unirest octetstring


    【解决方案1】:

    需要使用流而不是数据来写入文件。

    以下作品:

    fs.writeFile(`response.txt`, response.stream, function (error) {
      if (error) { console.error(error); }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 2012-05-20
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      • 2018-09-26
      相关资源
      最近更新 更多