21

在 Fiddler 中可以很好地捕获定期客户端发起的对节点服务器的请求。但是,不会捕获从节点发送到 Web 服务的请求。将代理(127.0.0.1:8888)的配置传递给请求方法没有帮助。如何通过 Fiddler 路由请求消息?

var http = require('http');
var request = require('request');

request.get(webserviceURL, { "auth" : {"user": "user", "pass" = "pass", sendImmediately: true },
"proxy" : { "host" : "127.0.0.1", "port" : 8888 }},
function (error, response) { console.log( "response received" );
});

请求回购:https ://github.com/mikeal/request

4

5 回答 5

26

我只是自己尝试这样做(使用 Fiddler 和 npm 的请求库)。这是我如何让它工作的:

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; // Ignore 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' authorization error

// Issue the request
request(
{
    method: "GET",
    uri: "https://secure.somewebsite.com/",
    proxy: "http://127.0.0.1:8888" // Note the fully-qualified path to Fiddler proxy. No "https" is required, even for https connections to outside.
},
function(err, response, body) {
    console.log("done");
});

这是 Fiddler2 使用默认端口和代理选项(并且没有代理身份验证)。

于 2014-04-10T18:25:52.953 回答
5

Fiddler 通过将“Internet 选项”(从开始菜单)“连接”>“LAN 设置”>“代理服务器”设置为其端口来工作,从而使所有 HTTP 流量(遵守此设置的客户端)通过它。

您应该将 node.js 客户端库指向使用代理,启动 Fiddler 后,设置将写入该选项对话框中。

于 2013-06-29T18:59:03.437 回答
4

代理选项应该是一个完整的 url,如下所示:

proxy : "http://127.0.0.1:8888"
于 2014-04-08T12:21:23.210 回答
3

要在不更改代码的情况下临时执行此操作,您可以使用环境变量。

请求尊重:

  • HTTP_PROXY
  • HTTPS_PROXY
  • NO_PROXY

因此,在运行进程之前,只需在控制台中设置这些代理即可。

例如,要设置 http 和 https 代理,请使用:

set HTTP_PROXY="http://127.0.0.1:8888"
set HTTPS_PROXY="http://127.0.0.1:8888"
set NODE_TLS_REJECT_UNAUTHORIZED=0

后一行通过提琴手代理停止了 SSL 问题。

于 2017-04-10T11:31:55.107 回答
0

我一直想要同样的东西……相当于Networkchrome DevTools 中的选项卡,仅适用于 Nodejs。不幸的是,它似乎并不存在。我在 macos 上没有 Fiddler,所以这就是我如何存根require('http')记录和传递的方法。把它留在这里以防我再次需要它或其他人觉得它有帮助。您可以通过附加调试器和require('filename')()包含此脚本的文件来打开它。

module.exports = () => {
    const http = require('http');
    http._request = http.request;

    global.DO_LOG_AJAX = true;
    const log = str => {
        if (global.DO_LOG_AJAX) {
            console.debug(str);
        }
    };

    const flushLog = (requestLines, responseLines) => {
        if (global.DO_LOG_AJAX) {
            log([
                '----------------Begin Request-----------------------------------',
                ...requestLines,
                '----------------End Request / Begin Response--------------------',
                ...responseLines,
                '----------------End Reponse-------------------------------------',
            ].join('\n'));
        }
    };

    let write;
    let end;
    http.request = (...requestParams) => {
        const req = http._request(...requestParams);
        const { method, path, headers, host, port } = requestParams[0];
        const requestLogLines = [];
        requestLogLines.push(`${method} ${path}`);
        requestLogLines.push(`Host: ${host}:${port}`);
        for (const header of Object.keys(headers)) {
            requestLogLines.push(`${header}: ${headers[header]}`);
        }
        write = write || req.write;
        end = end || req.end;

        req.on('error', err => {
            log({ err });
        });

        req._write = write;
        req._end = end;
        const requestBody = [];
        req.write = (...writeParams) => {
            requestBody.push(writeParams[0].toString());
            return req._write(...writeParams);
        };
        req.end = (...endParams) => {
            if (endParams[0]) {
                requestBody.push(endParams[0].toString());
            }
            requestLogLines.push('');
            requestLogLines.push(requestBody.join(''));
            return req._end(...endParams);
        };

        const responseLogLines = [];
        req.once('response', response => {
            const responseBody = [];
            responseLogLines.push(`${response.statusCode} ${response.statusMessage}`);
            for (const header of Object.keys(response.headers)) {
                responseLogLines.push(`${header}: ${response.headers[header]}`);
            }
            const onData = chunk => {
                responseBody.push(chunk.toString());
            };
            const onClose = err => {
                responseLogLines.push('');
                responseLogLines.push(responseBody.join(''));
                responseLogLines.push('');
                responseLogLines.push(`--- ERROR --- ${err.toString()}`);
                flushLog(requestLogLines, responseLogLines);
                req.removeListener('data', onData);
            };
            const onEnd = () => {
                responseLogLines.push('');
                responseLogLines.push(responseBody.join(''));
                flushLog(requestLogLines, responseLogLines);
                req.removeListener('data', onData);
            };
            response.on('data', onData);
            response.once('close', onClose);
            response.once('end', onEnd);
        });

        return req;
    };
};

于 2018-10-19T00:12:20.930 回答