如何 运行 Node.js 模块 'windows-1252' 以及 require 语句
How to run Node.js module 'windows-1252' along with require statements
尝试在 Windows 10 AWS EC2 上使用 Node.js 和 Newman 从 POST 请求中正确写入来自 data.response.stream 的 .json 文件。默认编码是cp1252,但是响应编码是utf-8,试过iconv,iconv-lite,futzing Buffer,似乎都没有得到满意的结果
这是我使用的代码:
const newman = require('newman'); // require Newman in the project
const fs = require('fs'); // require the Node.js module 'File System'
var url = require('url');
const https = require('https');
var path = require('path');
const { Iconv } = require('iconv').Iconv;
const iconvlite = require('iconv-lite');
const utf8 = require('utf8');
const windows1252 = require('windows-1252');
var requirejs = require('requirejs');
//import {encode, decode, labels} from 'windows-1252';
//import * as windows1252 from 'windows-1252';
//const collectionURL = 'https://www.getpostman.com/collections/mycollection';
let pageNumber = 16287;
// call newman.run to pass `options` object and wait for callback
newman.run({
collection: require('./postman-collection.json'),
iterationData: './iteration-data.csv',
color: 'on',
verbose: 'on',
exportCollection: './/after_pmRuns',
delayRequest: 500,
environment: require('./postman-environment.json'),
reporters: 'cli',
}).on('request', (error, data) => {
if (error) {
console.log(error);
return;
}
console.log('Request name: ' + data.item.name);
console.log(data.response.stream.toString());
var currentPageNumber = pageNumber++;
const requestName = data.item.name.replace(/[^a-z0-9]/gi, '-');
const randomString = Math.random().toString(36).substring(7);
const fileName = `./results/_00${currentPageNumber}-response-${requestName}-${randomString}.json`;
const encodedData = windows1252.encode(data.response.stream.toString(), {
mode: 'fatal'
});
const decodedData = iconvlite.decode(encodedData, 'utf-8');
//var iconv = new Iconv('windows-1252', 'UTF-8//TRANSLIT//IGNORE');
//var content = data.response.stream;
//var buffer = iconv.convert(content);
//var new_content = buffer.toString('utf8')
//const win = iconvlite.encode(data.response.stream, "windows1252");
//const utfStr = iconvlite.decode(win, "utf-8");
//const requestContent = data.response.stream.toString();
//return str.toString();
fs.writeFileSync(fileName, decodedData, function(error) {
if (error) {
console.error(error);
}
});
});
我在记事本中打开后不断收到 ascii 编码的 .json 文件。偶尔我会得到替换字符,如 \ufffd 或它的变体。
当我尝试调整 package.json 时,我纽曼抛出一个错误,因为它在 require 语句中,但是当我尝试导入 windows-1252 时,它说它是未定义的。
关于如何解决这个问题的任何想法?
我希望我们不需要编码或解码响应数据,我们可以简单地使用“解析”来缓冲数据以响应json。
JSON.parse(响应数据);
尝试在 Windows 10 AWS EC2 上使用 Node.js 和 Newman 从 POST 请求中正确写入来自 data.response.stream 的 .json 文件。默认编码是cp1252,但是响应编码是utf-8,试过iconv,iconv-lite,futzing Buffer,似乎都没有得到满意的结果
这是我使用的代码:
const newman = require('newman'); // require Newman in the project
const fs = require('fs'); // require the Node.js module 'File System'
var url = require('url');
const https = require('https');
var path = require('path');
const { Iconv } = require('iconv').Iconv;
const iconvlite = require('iconv-lite');
const utf8 = require('utf8');
const windows1252 = require('windows-1252');
var requirejs = require('requirejs');
//import {encode, decode, labels} from 'windows-1252';
//import * as windows1252 from 'windows-1252';
//const collectionURL = 'https://www.getpostman.com/collections/mycollection';
let pageNumber = 16287;
// call newman.run to pass `options` object and wait for callback
newman.run({
collection: require('./postman-collection.json'),
iterationData: './iteration-data.csv',
color: 'on',
verbose: 'on',
exportCollection: './/after_pmRuns',
delayRequest: 500,
environment: require('./postman-environment.json'),
reporters: 'cli',
}).on('request', (error, data) => {
if (error) {
console.log(error);
return;
}
console.log('Request name: ' + data.item.name);
console.log(data.response.stream.toString());
var currentPageNumber = pageNumber++;
const requestName = data.item.name.replace(/[^a-z0-9]/gi, '-');
const randomString = Math.random().toString(36).substring(7);
const fileName = `./results/_00${currentPageNumber}-response-${requestName}-${randomString}.json`;
const encodedData = windows1252.encode(data.response.stream.toString(), {
mode: 'fatal'
});
const decodedData = iconvlite.decode(encodedData, 'utf-8');
//var iconv = new Iconv('windows-1252', 'UTF-8//TRANSLIT//IGNORE');
//var content = data.response.stream;
//var buffer = iconv.convert(content);
//var new_content = buffer.toString('utf8')
//const win = iconvlite.encode(data.response.stream, "windows1252");
//const utfStr = iconvlite.decode(win, "utf-8");
//const requestContent = data.response.stream.toString();
//return str.toString();
fs.writeFileSync(fileName, decodedData, function(error) {
if (error) {
console.error(error);
}
});
});
我在记事本中打开后不断收到 ascii 编码的 .json 文件。偶尔我会得到替换字符,如 \ufffd 或它的变体。
当我尝试调整 package.json 时,我纽曼抛出一个错误,因为它在 require 语句中,但是当我尝试导入 windows-1252 时,它说它是未定义的。
关于如何解决这个问题的任何想法?
我希望我们不需要编码或解码响应数据,我们可以简单地使用“解析”来缓冲数据以响应json。
JSON.parse(响应数据);