mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
refactor: http requests
This commit is contained in:
parent
745cb0175c
commit
caa2fca4e8
@ -30,16 +30,16 @@ export class RequestUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 请求和回复都是JSON data传原始内容 自动编码json
|
// 请求和回复都是JSON data传原始内容 自动编码json
|
||||||
static async HttpGetJson<T>(url: string, method: string = 'GET', data?: any, headers: Record<string, string> = {}) {
|
static async HttpGetJson<T>(url: string, method: string = 'GET', data?: any, headers: Record<string, string> = {}): Promise<T> {
|
||||||
|
let option = new URL(url);
|
||||||
const protocol = url.startsWith('https://') ? https : http;
|
const protocol = url.startsWith('https://') ? https : http;
|
||||||
const options = {
|
const options = {
|
||||||
hostname: url.replace(/^(https?:\/\/)?(.*?)(\/.*)?$/, '$2'),
|
hostname: option.hostname,
|
||||||
port: url.startsWith('https://') ? 443 : 80,
|
port: option.port,
|
||||||
path: url.replace(/^(https?:\/\/[^\/]+)?(.*?)(\/.*)?$/, '$3'),
|
path: option.href,
|
||||||
method,
|
method,
|
||||||
headers,
|
headers,
|
||||||
};
|
};
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const req = protocol.request(options, (res: any) => {
|
const req = protocol.request(options, (res: any) => {
|
||||||
let responseBody = '';
|
let responseBody = '';
|
||||||
@ -64,11 +64,9 @@ export class RequestUtil {
|
|||||||
req.on('error', (error: any) => {
|
req.on('error', (error: any) => {
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (method === 'POST' || method === 'PUT' || method === 'PATCH') {
|
if (method === 'POST' || method === 'PUT' || method === 'PATCH') {
|
||||||
req.write(JSON.stringify(data));
|
req.write(JSON.stringify(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
req.end();
|
req.end();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -22,8 +22,8 @@ export async function postLoginStatus() {
|
|||||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0'
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0'
|
||||||
});
|
});
|
||||||
resolve(true);
|
resolve(true);
|
||||||
} catch {
|
} catch (e: any) {
|
||||||
reject('umami post failed')
|
reject(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user