requestUtil.js
1000 字节
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
41
const request = require('request')
const xml2js = require('xml2js');
const builder = new xml2js.Builder(); // JSON->xml
const parser = new xml2js.Parser(); //xml -> json
function requestUtil() {
}
requestUtil.prototype.post = (url,d)=>{
return new Promise((resolve, reject) =>{
var option = {
url: url,
method: "POST",
headers: {
"Content-Type":"application/xml"
},
body: d
};
request(option,(err,res,data)=>{
if(err){
reject(err)
}
resolve(data)
})
})
}
requestUtil.prototype.json2xml = (xml) =>{
return new Promise((resolve, reject) =>{
parser.parseString(xml,(err,data)=>{
if(err){
reject(err);
}
resolve(data)
});
})
}
module.exports = new requestUtil();