financesaucer
financesaucer•10mo ago

Axios HttpsProxyAgent

Hello, I tryed to search but found nothing. I'm using axios with httpProxyAgent to proxy my outgoing requests and with node everything works correctly, when running with deno it uses my ip instead of proxy ip, how can i fix it? maybe i should use fetch with proxy?
8 Replies
Deno AI Helper
Deno AI Helper•10mo ago
Hello! In Deno, you can use proxies for module downloads and the Web standard fetch API. The proxy configuration is read from environmental variables: HTTP_PROXY, HTTPS_PROXY, and NO_PROXY. On Windows, if environment variables are not found, Deno falls back to reading proxies from the registry, as stated in the documentation. If you're using fetch in Deno and want to use a proxy, you should set the appropriate environment variables before running your Deno script. Here's how you can set them in a Unix-like shell:
export HTTP_PROXY=http://proxy.example.com:8080
export HTTPS_PROXY=https://proxy.example.com:8080
export HTTP_PROXY=http://proxy.example.com:8080
export HTTPS_PROXY=https://proxy.example.com:8080
And here's how you can set them in Windows Command Prompt:
set HTTP_PROXY=http://proxy.example.com:8080
set HTTPS_PROXY=https://proxy.example.com:8080
set HTTP_PROXY=http://proxy.example.com:8080
set HTTPS_PROXY=https://proxy.example.com:8080
After setting these environment variables, Deno's fetch should respect the proxy settings when making HTTP/HTTPS requests. There's no need to use httpProxyAgent as you would in Node.js, since Deno's fetch is designed to work with these environment variables directly. If you're still having issues with the proxy after setting the environment variables, please ensure that the proxy server is correctly configured and that the environment variables are set in the context where Deno is running. 🦕
financesaucer
financesaucer•9mo ago
Thanks i actually did it with Deno.env.set using fetch and it works but with axios it does not work still, but no problem ill just rewrite all with fetch I have another probelm actually on deno deploy, it seems that env are not working correctly, im exiting with the instance ip instead of the proxy... i did set the env in the env section but it does not work, also tryed to get them and set them directly in the code with Deno.env.get / Deno.env.set How do i use fetch with proxy with deno deploy? its not working with env variable
Deno AI Helper
Deno AI Helper•9mo ago
In Deno Deploy, you don't have direct control over environment variables in the same way you do in a local environment. As of my knowledge cutoff in early 2023, Deno Deploy does not provide a built-in way to configure an HTTP proxy for outgoing requests made using fetch. However, you can manually implement proxy support in your code by modifying the request URL to go through a proxy server. This typically involves changing the request URL to the proxy URL and setting the appropriate headers so that the proxy can route your request to the intended destination. Here's a basic example of how you might send a request through a proxy using fetch:
const proxyUrl = 'http://your-proxy-server.com:port';
const targetUrl = 'http://your-target-url.com';

const response = await fetch(proxyUrl, {
method: 'POST', // or 'GET', 'PUT', etc.
headers: {
'Content-Type': 'application/json',
// Additional headers as required by your proxy
},
body: JSON.stringify({
url: targetUrl,
// Any other proxy-specific payload data
}),
});

const data = await response.json();
console.log(data);
const proxyUrl = 'http://your-proxy-server.com:port';
const targetUrl = 'http://your-target-url.com';

const response = await fetch(proxyUrl, {
method: 'POST', // or 'GET', 'PUT', etc.
headers: {
'Content-Type': 'application/json',
// Additional headers as required by your proxy
},
body: JSON.stringify({
url: targetUrl,
// Any other proxy-specific payload data
}),
});

const data = await response.json();
console.log(data);
In this example, you would need to replace http://your-proxy-server.com:port with the actual URL of your proxy server and include any headers or body content that your proxy expects. Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of this advice and ensure that it aligns with the specifics of your proxy server's requirements. If you're using a third-party proxy service, they may have specific instructions or SDKs that you can use to route your requests through their proxy.
financesaucer
financesaucer•9mo ago
how to use unstable api on deno deploy
Deno AI Helper
Deno AI Helper•9mo ago
As of my last update, Deno Deploy does not support the use of unstable APIs. Deno Deploy is designed to run scripts in a secure and standardized environment, and unstable APIs are typically not included in this environment because they are subject to change and may not be reliable for production use. The unstable flag (--unstable) in the Deno CLI allows access to Deno's unstable features on a local machine. However, this flag is not applicable to Deno Deploy, as you do not have the same level of control over the runtime environment. If you are relying on unstable features for your application, you may need to look for alternative stable APIs or workarounds that are compatible with Deno Deploy. Keep an eye on the official Deno release notes and documentation for updates on when and if certain unstable features become stable and are included in Deno Deploy. Remember to always test your application thoroughly when using any new or updated features, whether stable or unstable, to ensure compatibility and reliability in your deployment environment.
financesaucer
financesaucer•9mo ago
so how can i make fetch requests with a proxy using stable api, proxy env variables seems to not be working with deno deploy and also i cant use deno.createHttpClient because its unstable api
Deno AI Helper
Deno AI Helper•9mo ago
In Deno Deploy, since you cannot use environment variables for proxy configuration or the unstable Deno.createHttpClient API, you would need to handle proxying at the application level. This means manually configuring your fetch requests to route through a proxy server. Here's a general approach you can take, keeping in mind that this is not directly from the documentation, and you may need to adjust it based on the specifics of your proxy server: 1. Set up a proxy server that can handle forwarding requests to the desired destination. 2. Modify your fetch requests to point to your proxy server. 3. Include any necessary headers or body content that your proxy server requires to forward the request to the final destination. Here's a conceptual example of how you might modify a fetch request to use a proxy:
// This is a conceptual example and may not work out-of-the-box.
// Replace with your actual proxy server details and target URL.
const proxyUrl = 'https://your-proxy-server.com';
const targetUrl = 'https://your-final-destination.com';

// You may need to include additional headers or body content
// depending on how your proxy server expects to receive the request.
const response = await fetch(proxyUrl, {
method: 'POST', // or 'GET', 'PUT', etc., depending on your proxy server's requirements
headers: {
'Content-Type': 'application/json',
// Additional headers required by your proxy
},
body: JSON.stringify({
url: targetUrl,
// Any other data your proxy needs to forward the request
}),
});

const data = await response.json();
console.log(data);
// This is a conceptual example and may not work out-of-the-box.
// Replace with your actual proxy server details and target URL.
const proxyUrl = 'https://your-proxy-server.com';
const targetUrl = 'https://your-final-destination.com';

// You may need to include additional headers or body content
// depending on how your proxy server expects to receive the request.
const response = await fetch(proxyUrl, {
method: 'POST', // or 'GET', 'PUT', etc., depending on your proxy server's requirements
headers: {
'Content-Type': 'application/json',
// Additional headers required by your proxy
},
body: JSON.stringify({
url: targetUrl,
// Any other data your proxy needs to forward the request
}),
});

const data = await response.json();
console.log(data);
Please note that this is a general advice and not a specific solution from the documentation. You will need to consult the documentation or support resources for your proxy
Deno AI Helper
Deno AI Helper•9mo ago
server to understand exactly how to format your requests. If you're using a third-party proxy service, they may provide specific instructions or even an SDK that you can use to route your requests through their proxy. Always ensure that you handle sensitive information, such as API keys or credentials, securely and in accordance with best practices. 🦕