WMS_MAIN/script/api-test2.js

48 lines
1.3 KiB
JavaScript

const http = require('http');
const token = process.argv[2];
const method = process.argv[3]; // e.g. order.pendingOrder.createOrder
const reqBodyFile = process.argv[4]; // path to JSON file or JSON string
let reqBody;
if (reqBodyFile && reqBodyFile.endsWith('.json')) {
reqBody = require('fs').readFileSync(reqBodyFile, 'utf8');
} else {
reqBody = reqBodyFile || '{}';
}
const options = {
hostname: 'localhost',
port: 6060,
path: `/system?method=${method}&v=1&timestamp=${Math.floor(Date.now() / 1000)}`,
method: 'POST',
headers: {
platform: 'web',
project: 'wms',
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
};
const req = http.request(options, res => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
console.log(`STATUS: ${res.statusCode}`);
console.log('HEADERS:', JSON.stringify(res.headers));
try {
const json = JSON.parse(data);
console.log(JSON.stringify(json, null, 2));
} catch (e) {
console.log('RAW BODY (first 2000 chars):');
console.log(data.substring(0, 2000));
if (data.length > 2000) {
console.log('...(truncated, total length: ' + data.length + ')');
}
}
});
});
req.on('error', e => console.error('REQUEST ERROR:', e.message));
req.write(reqBody);
req.end();