nodejs 中 axios 设置 burp 抓取 http 与 https

在使用 axios 库的时候,希望用 burp 抓包查看发包内容。但关于 axios 设置代理问题,网上提到的一些方法不是好用,摸索了一段时间后总结出设置 burp 代理抓包的方法。

nodejs 中 axios 设置 burp 抓包

根据请求的站点,分为 http 和 https 两个类型。

http

只需要添加 proxy

js 复制代码
// http 测试网站: http://www.5icool.org/

import https from "https";
import axios from "axios";

const proxy = {
    protocol: 'http',   // 这里设置协议为 http
    host: '127.0.0.1',
    port: 8080
}


async function test() {
    const res = await axios.post("http://www.5icool.org/", {
        title: 'foo',
        body: 'bar',
        userId: 1,
    }, {
        proxy: proxy   // http 站点,就直接设置  proxy 参数
        headers: {'Content-type': 'application/json; charset=UTF-8'},
    })
    console.log(res.data)

}

await test();

https

要添加 proxy 以及 httpsAgent

js 复制代码
// https 测试站点: https://jsonplaceholder.typicode.com/posts

import https from "https";
import axios from "axios";
let httpsAgent = new https.Agent({
    rejectUnauthorized: false,   // 因为是 https over http ,所以需要设置 rejectUnauthorized 为 false
});
const proxy = {
    protocol: 'https',   // 这里要设置 https 
    host: '127.0.0.1',
    port: 8080
}


async function test() {
    const res = await axios.post("https://jsonplaceholder.typicode.com/posts", {
        title: 'foo',
        body: 'bar',
        userId: 1,
    }, {
        httpsAgent: httpsAgent,  // 添加 httpsAgent
        proxy: proxy,   // 添加 proxy
        headers: {'Content-type': 'application/json; charset=UTF-8'},
    })
    console.log(res.data)

}

await test();
相关推荐
小菜摸鱼7 小时前
Node.js + vue3 大文件-切片上传全流程(视频文件)
前端·node.js
PaytonD10 小时前
LoopBack 2 如何设置静态资源缓存时间
前端·javascript·node.js
许久'14 小时前
环境搭建node.js gnvm
node.js
细节控菜鸡15 小时前
Webpack 核心知识点详解:proxy、热更新、Loader与Plugin全解析
前端·webpack·node.js
spmcor15 小时前
Nest.js 文件分片上传:当大文件来敲门,别慌,我们有“分尸”妙计!
node.js
星空下的曙光1 天前
Node.js crypto模块所有 API 详解 + 常用 API + 使用场景
算法·node.js·哈希算法
青灬河1 天前
实现企业级全栈应用服务框架-Elpis(一)
vue.js·node.js
星空下的曙光1 天前
Node.js events模块所有 API 详解 + 常用 API + 使用场景
node.js
无责任此方_修行中1 天前
我的两次 Vibe Coding 经历,一次天堂,一次地狱
后端·node.js·vibecoding
程序铺子2 天前
如何使用 npm 安装 sqlite3 和 canvas 这些包
javascript·npm·node.js