MCPcopy Index your code
hub / github.com/YMFE/yapi / httpRequestByNode

Function httpRequestByNode

common/postmanLib.js:54–125  ·  view source on GitHub ↗
(options)

Source from the content-addressed store, hash-verified

52}
53
54async function httpRequestByNode(options) {
55 function handleRes(response) {
56 if (!response || typeof response !== 'object') {
57 return {
58 res: {
59 status: 500,
60 body: isNode
61 ? '请求出错, 内网服务器自动化测试无法访问到,请检查是否为内网服务器!'
62 : '请求出错'
63 }
64 };
65 }
66 return {
67 res: {
68 header: response.headers,
69 status: response.status,
70 body: response.data
71 }
72 };
73 }
74
75 function handleData() {
76 let contentTypeItem;
77 if (!options) return;
78 if (typeof options.headers === 'object' && options.headers) {
79 Object.keys(options.headers).forEach(key => {
80 if (/content-type/i.test(key)) {
81 if (options.headers[key]) {
82 contentTypeItem = options.headers[key]
83 .split(';')[0]
84 .trim()
85 .toLowerCase();
86 }
87 }
88 if (!options.headers[key]) delete options.headers[key];
89 });
90
91 if (
92 contentTypeItem === 'application/x-www-form-urlencoded' &&
93 typeof options.data === 'object' &&
94 options.data
95 ) {
96 options.data = qs.stringify(options.data);
97 }
98 }
99 }
100
101 try {
102 handleData(options);
103 let response = await axios({
104 method: options.method,
105 url: options.url,
106 headers: options.headers,
107 timeout: 10000,
108 maxRedirects: 0,
109 httpsAgent: new https.Agent({
110 rejectUnauthorized: false
111 }),

Callers 2

crossRequestFunction · 0.85

Calls 2

handleResFunction · 0.85
handleDataFunction · 0.70

Tested by

no test coverage detected