From 03171f5dd6103ebfaa20345625467d124677592c Mon Sep 17 00:00:00 2001 From: Jeremie Thomassey <44839746+JitixPhotobox@users.noreply.github.com> Date: Tue, 24 Sep 2019 11:27:33 +0200 Subject: [PATCH] Make redirection from HTTP to HTTPS work When calling an HTTP resource redirecting to a HTTPS one with a keepAlive agent. We get the following error: ``` TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:" at new ClientRequest (_http_client.js:119:11) at Object.request (https.js:281:10) at RedirectableRequest._performRequest (/Users/jthomassey/projects/ecom-shop-web/node_modules/follow-redirects/index.js:169:24) at RedirectableRequest._processResponse (/Users/jthomassey/projects/ecom-shop-web/node_modules/follow-redirects/index.js:260:10) at ClientRequest.RedirectableRequest._onNativeResponse (/Users/jthomassey/projects/ecom-shop-web/node_modules/follow-redirects/index.js:50:10) at Object.onceWrapper (events.js:277:13) at ClientRequest.emit (events.js:189:13) at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:556:21) at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17) at Socket.socketOnData (_http_client.js:442:20) ``` This can be tested here : ``` const http = require('http'); const https = require('https'); const axios = require('axios'); axios.get('http://www.photobox.fr', { httpAgent: http.Agent({ keepAlive:true }), httpsAgent: https.Agent({ keepAlive:true }) }) .then(response => { console.log(response); console.log(response.headers); }) .catch(error => { console.log(error); }); ``` Axios delegate the redirection to the follow-redirect package which accept an option `agents` for both http and https agent see : https://github.com/follow-redirects/follow-redirects#per-request-options --- lib/adapters/http.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/adapters/http.js b/lib/adapters/http.js index 38f92e43f2..d3b1191a33 100755 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -85,6 +85,10 @@ module.exports = function httpAdapter(config) { method: config.method.toUpperCase(), headers: headers, agent: agent, + agents: { + httpsAgent: config.httpsAgent, + httpAgent: config.httpAgent + }, auth: auth };