Skip to content
This repository was archived by the owner on Jul 12, 2019. It is now read-only.

Commit 2c016ef

Browse files
committed
Merge pull request danwrong#109 from max-koehler/HTTP-Location-Fix
Fixed redirect handling for HTTP status code 303
2 parents 9358eb0 + 14e2284 commit 2c016ef

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.swp
22
.[Dd][Ss]_store
33
node_modules
4+
.idea

lib/restler.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,18 @@ mixin(Request.prototype, {
119119

120120
if (self._isRedirect(response) && self.options.followRedirects) {
121121
try {
122-
self.url = url.parse(url.resolve(self.url.href, response.headers['location']));
123-
self._retry();
124-
// todo handle somehow infinite redirects
122+
// 303 should redirect and retrieve content with the GET method
123+
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4
124+
if (response.statusCode === 303) {
125+
self.url = url.parse(url.resolve(self.url.href, response.headers['location']));
126+
self.options.method = 'GET';
127+
delete self.options.data;
128+
self._retry();
129+
} else {
130+
self.url = url.parse(url.resolve(self.url.href, response.headers['location']));
131+
self._retry();
132+
// todo handle somehow infinite redirects
133+
}
125134
} catch(err) {
126135
err.message = 'Failed to follow redirect: ' + err.message;
127136
self._fireError(err, response);
@@ -229,7 +238,6 @@ mixin(Request.prototype, {
229238
},
230239
run: function() {
231240
var self = this;
232-
233241
if (this.options.multipart) {
234242
multipart.write(this.request, this.options.data, function() {
235243
self.request.end();
@@ -326,7 +334,7 @@ function json(url, data, options, method) {
326334
options.parser = (typeof options.parser !== "undefined") ? options.parser : parsers.auto;
327335
options.headers = options.headers || {};
328336
options.headers['content-type'] = 'application/json';
329-
options.data = JSON.stringify(data);
337+
options.data = JSON.stringify(data || {});
330338
options.method = method || 'GET';
331339
return request(url, options);
332340
}
@@ -497,3 +505,4 @@ mixin(exports, {
497505
file: multipart.file,
498506
data: multipart.data
499507
});
508+

0 commit comments

Comments
 (0)