Why can't I read deno proxy environment variable on Windows 10?

Viewed 357

I'm on Windows 10 and read the deno manual that says you need to set up the environment variable HTTPS_PROXY so that fetch() will use the proxy.

I did so by doing

Deno.env.set('HTTP_PROXY', 'https://138.68.60.8:3128/');
Deno.env.set('HTTPS_PROXY', 'https://138.68.60.8:3128/');

yet the fetch still fetches without the proxy. Instead, if I set up a proxy in the Windows system setting, the fetch will use it. Which doesn't solve the problem though as I am trying to a proxy list rolling.

1 Answers

Looking at the Deno source code, their own tests use a different method that I couldn't find in the documentation:

// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

const client = Deno.createHttpClient({
  proxy: {
    url: "http://localhost:4555",
    basicAuth: { username: "username", password: "password" },
  },
});

const res = await fetch(
  "http://localhost:4545/045_mod.ts",
  { client },
);
console.log(`Response http: ${await res.text()}`);

client.close();

For your specific use case, I would setup a http client:

const client = Deno.createHttpClient({
  proxy: {
    url: "https://138.68.60.8:3128",
  },
});

And then use that client on your fetch requests:

fetch("https://example.com", { client });
Related