Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: abh/restler
...
head fork: abh/restler
compare: multipart-content-length
Checking mergeability… Don’t worry, you can still create the pull request.
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
4 README.md
@@ -147,14 +147,14 @@ rest.post('http://user:pass@service.com/action', {
}
});
-// multipart request sending a file and using https
+// multipart request sending a 321567 byte long file using https
rest.post('https://twaud.io/api/v1/upload.json', {
multipart: true,
username: 'danwrong',
password: 'wouldntyouliketoknow',
data: {
'sound[message]': 'hello from restler!',
- 'sound[file]': rest.file('doug-e-fresh_the-show.mp3', null, null, null, 'audio/mpeg')
+ 'sound[file]': rest.file('doug-e-fresh_the-show.mp3', null, 321567, null, 'audio/mpeg')
}
}).on('complete', function(data) {
sys.puts(data.audio_url);
View
2  lib/multipartform.js
@@ -87,6 +87,8 @@ Part.prototype = {
valueSize = this.value.fileSize;
} else if (this.value.data) {
valueSize = this.value.data.length;
+ } else if (typeof this.value === 'number') {
+ valueSize = this.value.toString().length;
} else {
valueSize = this.value.length;
}
View
7 lib/restler.js
@@ -55,6 +55,13 @@ function Request(uri, options) {
if (this.options.multipart) {
this.headers['Content-Type'] = 'multipart/form-data; boundary=' + multipart.defaultBoundary;
+ var multipart_size = multipart.sizeOf(this.options.data, multipart.defaultBoundary);
+ if (typeof multipart_size === 'number' && multipart_size === multipart_size) {
+ this.headers['Content-Length'] = multipart_size;
+ }
+ else {
+ console.log("Building multipart request without Content-Length header, please specify all file sizes");
+ }
} else {
if (typeof this.options.data == 'object') {
this.options.data = qs.stringify(this.options.data);
View
6 test/restler.js
@@ -277,12 +277,14 @@ module.exports['Multipart'] = {
'Test multipart request with simple vars': function(test) {
rest.post(host, {
- data: { a: 1, b: 'thing' },
+ data: { a: 10, b: 'thing' },
multipart: true
}).on('complete', function(data) {
test.re(data, /content-type\: multipart\/form-data/, 'should set "content-type" header')
- test.re(data, /name="a"(\s)+1/, 'should send a=1');
+ test.re(data, /name="a"(\s)+10/, 'should send a=10');
test.re(data, /name="b"(\s)+thing/, 'should send b=thing');
+ test.re(data, /content-length: 200/, 'should send content-length header');
+
test.done();
});
}

No commit comments for this range

Something went wrong with that request. Please try again.