axios 封装
import axios, { AxiosInstance, AxiosResponse } from 'axios';
/**
* 基于Axios框架实现的Http工具
*/
export class HttpUtils {
private readonly axios: AxiosInstance;
constructor(_timeout: number = 5000) {
this.axios = axios.create({
timeout: _timeout
});
}
async postSSL(url: string, data: any = {}, headers: object = {}, contentType: string, httpsAgent): Promise<AxiosResponse> {
const config = {}
config['headers'] = headers;
config['headers']['content-type'] = contentType;
config['httpsAgent'] = httpsAgent;
const response: AxiosResponse = await this.axios.post(url, data, config);
return response
}
}
export default new HttpUtils()
测试
import * as https from 'https'
import * as fs from 'fs'
import HttpUtils from './HttpUtils'
const certPath = '/ext/aaa/bb/apiclient_cert.p12'
const cretPwd = '证书密码'
cosnt WX_REFUND_APPLY_API = '微信退款API地址'
const refundXML = '退款请求XML'
//测试
(
async()=>{
const pfx = fs.readFileSync('certPath');
const agent = new https.Agent({
pfx,
passphrase: apiCretPwd
});
const response = await HttpUtils.postSSL(WX_REFUND_APPLY_API, refundXML, {}, 'text/html', agent)
}
)()