diff --git a/README.md b/README.md index 15f3419..b0b9fa9 100644 --- a/README.md +++ b/README.md @@ -63,15 +63,16 @@ const { http, https } = require('follow-redirects'); const options = url.parse('http://bit.ly/900913'); options.maxRedirects = 10; -options.beforeRedirect = (options, { headers, statusCode, requestMethod, requestUrl }) => { +options.beforeRedirect = (options, { headers, statusCode }, { method, url }) => { // Use this to adjust the request options upon redirecting, // to inspect the latest response headers, // or to cancel the request by throwing an error // headers = the redirect response headers // statusCode = the redirect response code (eg. 301, 307, etc.) - // requestMethod = the request method that resulted in a redirect - // requestUrl = the requested URL that resulted in a redirect + + // method = the request method that resulted in a redirect + // url = the requested URL that resulted in a redirect if (options.hostname === "example.com") { options.auth = "user:password"; } diff --git a/index.js b/index.js index 0201acd..67ec9b5 100644 --- a/index.js +++ b/index.js @@ -366,7 +366,7 @@ RedirectableRequest.prototype._processResponse = function (response) { // care for methods not known to be safe, […] // RFC7231§6.4.2–3: For historical reasons, a user agent MAY change // the request method from POST to GET for the subsequent request. - var originalRequestMethod = this._options.method; + var method = this._options.method; if ((statusCode === 301 || statusCode === 302) && this._options.method === "POST" || // RFC7231§6.4.4: The 303 (See Other) status code indicates that // the server is redirecting the user agent to a different resource […] @@ -418,11 +418,18 @@ RedirectableRequest.prototype._processResponse = function (response) { var responseDetails = { headers: response.headers, statusCode: statusCode, - requestMethod: originalRequestMethod, - requestUrl: currentUrl, + }; + var requestDetails = { + method: method, + url: currentUrl, }; try { - this._options.beforeRedirect.call(null, this._options, responseDetails); + this._options.beforeRedirect.call( + null, + this._options, + responseDetails, + requestDetails + ); } catch (err) { this.emit("error", err); diff --git a/test/test.js b/test/test.js index 3b6ac8b..ede3e02 100644 --- a/test/test.js +++ b/test/test.js @@ -1628,8 +1628,8 @@ describe("follow-redirects", function () { port: 3600, path: "/a", method: "POST", - beforeRedirect: function (_, response) { - requestMethods.push(response.requestMethod); + beforeRedirect: function (_, __, request) { + requestMethods.push(request.method); }, }; http.get(options, concatJson(resolve, reject)).on("error", reject); @@ -1655,8 +1655,8 @@ describe("follow-redirects", function () { port: 3600, path: "/a", method: "GET", - beforeRedirect: function (_, response) { - urlChain.push(response.requestUrl); + beforeRedirect: function (_, __, request) { + urlChain.push(request.url); }, }; http.get(options, concatJson(resolve, reject)).on("error", reject);