Request to HTTPS with HTTP proxy fails
Problem
Summary Trying to do a HTTPS request with a HTTP proxy fails. [code block] Results in: [code block] The problem is already described by @chovy in this ticket, which ended up closed as OP did not have the same problem: https://github.com/mzabriskie/axios/issues/662 @chovy says: > I still have this issue not being able to hit an https url with an http proxy. I can do it fine in request and also in curl from shell. Something not working with axios. I get an EPROTO error. Context - axios version: v0.16.1 - Environment: node v7.10.0, Mac OSX Sierra
Error Output
Error: write EPROTO 140736379442112:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:794
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Configure Axios to Use HTTPS Proxy for HTTPS Requests
The error occurs because the HTTP proxy does not support HTTPS requests directly. When Axios attempts to make an HTTPS request through an HTTP proxy, it fails to establish a secure connection, leading to the EPROTO error. This is due to the proxy not being able to handle the SSL/TLS handshake required for HTTPS connections.
Awaiting Verification
Be the first to verify this fix
- 1
Install HTTPS Proxy Agent
To handle HTTPS requests through an HTTP proxy, you need to use an HTTPS proxy agent. Install the 'https-proxy-agent' package which will help Axios to route HTTPS requests correctly through the proxy.
bashnpm install https-proxy-agent - 2
Import and Configure HTTPS Proxy Agent
Import the HTTPS proxy agent in your code and configure it to be used with Axios. This will allow Axios to communicate with the HTTPS endpoint through the HTTP proxy.
javascriptconst axios = require('axios'); const HttpsProxyAgent = require('https-proxy-agent'); const proxyUrl = 'http://your-http-proxy:port'; const agent = new HttpsProxyAgent(proxyUrl); axios.get('https://your-https-url', { httpsAgent: agent }) - 3
Test the Configuration
Run your application to test if the HTTPS request through the HTTP proxy is successful. Ensure that the proxy URL is correct and that the proxy server is reachable.
bashnode your-script.js - 4
Handle Errors Gracefully
Implement error handling to manage potential issues when making requests through the proxy. This will help in debugging if the request fails again.
javascriptaxios.get('https://your-https-url', { httpsAgent: agent }) .then(response => console.log(response.data)) .catch(error => console.error('Error:', error.message));
Validation
Confirm the fix by successfully making an HTTPS request through the HTTP proxy and receiving the expected response. Check for any errors in the console and ensure that the response data is correct.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep