Skip to content

HttpsProxy does not work correctly because fetch seems runs unexpectedly in node backend environment #1437

@xzf0587

Description

@xzf0587

Bug Report

I tried to put a https proxy (for example fiddler) for graph client in node environment.
But I found the request seems not sent to the proxy, as I cannot observe the request in fiddler.
I tried to import "isomorphic-fetch"; as the guide shows, but it still failed.

If I customize the graph client by specifying the http message handler to use fetch from "isomorphic-fetch".
It works as I could observe the request in fiddler.

Here is the code. Using node 18

import { Client, Context, Middleware } from "@microsoft/microsoft-graph-client";
import { HttpsProxyAgent } from "hpagent";
import fetch from "isomorphic-fetch";

class HTTPMessageHandler implements Middleware {
	/**
	 * @public
	 * @async
	 * To execute the current middleware
	 * @param {Context} context - The request context object
	 * @returns A promise that resolves to nothing
	 */
	public async execute(context: Context): Promise<void> {
		context.response = await fetch(context.request, context.options);
	}
}

async function main() {
    try {
        const agent = new HttpsProxyAgent({
            proxy: 'http://127.0.0.1:8888', // Fiddler proxy address
            rejectUnauthorized: false,
          })

        const graphClient = Client.initWithMiddleware({middleware: new HTTPMessageHandler()});
        const api  = graphClient.api("/me")
        const result = await api.option("agent", agent).get();
        console.log(result);
    } catch (error: any) {
        console.log(error.message);
    }
}

main();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions