Axios 配置使用SSL证书 (微信退款)

该博客展示了如何基于axios库封装一个HTTP工具类,特别地,实现了SSL POST请求,并用于调用微信退款API。通过导入https和fs模块,读取证书文件并设置httpsAgent,然后使用HttpUtils.postSSL方法发送退款XML数据到指定API。
摘要由CSDN通过智能技术生成

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)
	}
)()

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值