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();
相关推荐
belldeep4 小时前
nodejs:Vite + Svelte + ts 入门示例
typescript·node.js·ts·vite·svelte
__zRainy__7 小时前
npx skills核心功能速查及技能开发指南
ai·node.js
Z_Wonderful9 小时前
npm -v无效PowerShell 的执行策略,解决方案
前端·npm·node.js
枫桥骤雨9 小时前
Ubuntu NVM部署Node.js教程
linux·ubuntu·node.js·nvm
qqacj10 小时前
node.js下载、安装、设置国内镜像源(永久)(Windows11)
node.js
csdn_aspnet10 小时前
使用 nvm-desktop 轻松安装和管理多个 Node.js 版本
node.js
光影少年10 小时前
Node.js的事件循环和浏览器有什么区别?
node.js·编辑器·vim
ZC19959210 小时前
Node.js npm 安装过程中 EBUSY 错误的分析与解决方案
前端·npm·node.js
Ama_tor10 小时前
PicGo无法安装插件| 提示“请安装 Node.js 并重启 PicGo 再继续操作”(问题已解决)
node.js