ropHttpClient.js
1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const http = require("http");
const https = require("https");
const qs = require("qs");
const sign = require("./signCore");
/**
* POST 到 ROP router,返回原始响应体 { code, data, message, errId }
*/
function postRop(baseURL, secret, data, options = {}) {
const body = qs.stringify(sign(data, options.noSignAttr || [], secret, options.signWay || "sha1"));
const url = new URL(baseURL);
const lib = url.protocol === "https:" ? https : http;
return new Promise((resolve, reject) => {
const req = lib.request({
hostname: url.hostname,
port: url.port || (url.protocol === "https:" ? 443 : 80),
path: url.pathname,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": Buffer.byteLength(body),
},
}, (res) => {
let raw = "";
res.on("data", (chunk) => { raw += chunk; });
res.on("end", () => {
try {
resolve(JSON.parse(raw));
} catch (error) {
reject(new Error(`ROP response parse failed: ${raw.slice(0, 300)}`));
}
});
});
req.on("error", reject);
req.write(body);
req.end();
});
}
module.exports = postRop;