关于
Buy Me a Coffee
归档
友链
猫咪
RSS
后台
切换模式
返回顶部
首页
说说
日常
开发
游戏
资源
虚拟货币
首页
说说
日常
开发
游戏
资源
虚拟货币
关于
Buy Me a Coffee
归档
友链
猫咪
RSS
后台
白嫖 Hoppscotch Proxy 实现请求访问
开发
·
06-01
LiesAuer
### Hoppscotch 是啥 [Hoppscotch](https://github.com/hoppscotch/hoppscotch "Hoppscotch") 是类似于 Postman 的开源 API 请求工具。其自带的 Proxy 可以让我们做到意想不到的事情。 ![微信截图_20240601114858.png](https://storage.liesauer.net/2024/06/3530419221.png) ![微信图片_20240601114626.png](https://storage.liesauer.net/2024/06/3527769043.png) ### 直接上才艺 ```shell npm i axios lodash.merge ``` #### 演示代码 ```javascript axiosWithHoppscotch().get('https://www.google.com/').then(response => { console.log(response.data); }); ``` ![微信截图_20240601115328.png](https://storage.liesauer.net/2024/06/2500102380.png) #### 核心代码 ```javascript import axios from "axios"; import merge from "lodash.merge"; function axiosWithHoppscotch() { const axiosInstance = axios.create({ transformRequest: axios.defaults.transformRequest, transformResponse: axios.defaults.transformResponse, }); axiosInstance.interceptors.request.use(config => { const raw = { lMethod: (config.method + "").toLowerCase(), method: (config.method + "").toUpperCase(), url: config.url, data: config.data, headers: structuredClone(config.headers), }; config.method = "POST"; config.url = "https://proxy.hoppscotch.io/"; config.headers = {}; config.headers["Origin"] = "https://hoppscotch.io"; config.headers["Referer"] = "https://hoppscotch.io/"; config.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"; /** * 对header进行剥离 * 并按照优先级进行合并 */ let rawHeader0; let rawHeaders; rawHeader0 = {}; rawHeaders = {}; for (const key in raw.headers) { const value = raw.headers[key]; if ((key == "common" || key == "delete" || key == "get" || key == "head" || key == "post" || key == "put" || key == "patch") && value && typeof value == "object") { rawHeader0[key] = value; continue; } rawHeaders[key] = value; } const headers = merge({}, rawHeader0.common || {}, rawHeader0[raw.lMethod] || {}, rawHeaders); // header键值对必须是string类型,不然请求会报错 for (const key in headers) { if (typeof headers[key] == 'string') continue; headers[key] = headers[key] + ''; } const data = { method: raw.method, url: raw.url, headers: headers, params: {}, data: raw.data ?? null, wantsBinary: false, accessToken: "", }; const multiPart = raw.method == "POST" && raw.data instanceof FormData; if (multiPart) { config.headers["Content-Type"] = "multipart/form-data"; const guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); const multiPartKey = "proxyRequestData-" + guid; config.headers["Multipart-Part-Key"] = multiPartKey; const formData = raw.data as FormData; data.data = ""; formData.set(multiPartKey, JSON.stringify(data)); config.data = formData; } else { config.headers["Content-Type"] = "application/json"; config.data = JSON.stringify(data); } return config; }); axiosInstance.interceptors.response.use(response => { const rawData = response.data; if (rawData.success) { response.status = rawData.status; response.statusText = rawData.statusText; response.data = rawData.data; response['header0'] = response.headers; response.headers = rawData.headers; const isBinary = rawData.isBinary; if (isBinary) { // TODO: decode data } } else { response.status = 400; response.statusText = "Bad Request"; response.data = rawData.data; } return response; }); return axiosInstance; } ```
Hoppscotch
如果您觉得文章或项目对您有帮助,戳我请博主喝一杯咖啡叭!
取消回复
提交评论
xiaohu
06-06
回复
牛逼一
瞅一瞅叭
域名出售
uterminal.ai
服务器推荐
RackNerd 2C2G $20.98/年(美国 隐藏款)
狗云 1C0.75G ¥150/年(香港 BGP)
最新评论
www: 配置文件填写后,运行exe 显示登录信息,然后就卡住了 ┌─...
从良未遂: 你好,我接入cloudflare后发现修改主题、发表文章、回复评...
colin: 所以用 windows 来开发是原罪
人: 不是怎么意外的连上外网?
志航: 可以试着把文件上传蓝奏云,这么干的样例很多
志航: QQ群加不进去
tinker: 为什么运行一段时间就会停止,重启又恢复!没有报错信息,频道信息和...
关于站长
广东 佛山
liesauer#liesauer.net
LiesAuer
CC BY-NC-SA 4.0
粤ICP备16094588号-1
萌ICP备20245567号
Theme
Jasmine
by
Kent Liao
牛逼一