Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Set Content-Length for multipart requests

Closes #64
  • Loading branch information...
commit 41cba2528f44567826914b489d638dcb91a1ce3f 1 parent ea820fa
@abh abh authored
Showing with 9 additions and 2 deletions.
  1. +2 −2 README.md
  2. +7 −0 lib/restler.js
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
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);
Please sign in to comment.
Something went wrong with that request. Please try again.