{"action":"created","issue":{"url":"https://api.github.com/repos/ringcentral/ringcentral-js/issues/15","repository_url":"https://api.github.com/repos/ringcentral/ringcentral-js","labels_url":"https://api.github.com/repos/ringcentral/ringcentral-js/issues/15/labels{/name}","comments_url":"https://api.github.com/repos/ringcentral/ringcentral-js/issues/15/comments","events_url":"https://api.github.com/repos/ringcentral/ringcentral-js/issues/15/events","html_url":"https://github.com/ringcentral/ringcentral-js/issues/15","id":143869324,"number":15,"title":"Unable to fax a file from file system.","user":{"login":"tylerlong","id":733544,"avatar_url":"https://avatars.githubusercontent.com/u/733544?v=3","gravatar_id":"","url":"https://api.github.com/users/tylerlong","html_url":"https://github.com/tylerlong","followers_url":"https://api.github.com/users/tylerlong/followers","following_url":"https://api.github.com/users/tylerlong/following{/other_user}","gists_url":"https://api.github.com/users/tylerlong/gists{/gist_id}","starred_url":"https://api.github.com/users/tylerlong/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tylerlong/subscriptions","organizations_url":"https://api.github.com/users/tylerlong/orgs","repos_url":"https://api.github.com/users/tylerlong/repos","events_url":"https://api.github.com/users/tylerlong/events{/privacy}","received_events_url":"https://api.github.com/users/tylerlong/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-03-28T01:37:23Z","updated_at":"2016-03-28T02:29:49Z","closed_at":null,"body":"Please read this issue first: https://github.com/ringcentral/ringcentral-js/issues/13\r\n\r\nThe fax feature didn't work. Now it works but I still fail to fax a file from file system.\r\n\r\nSample code:\r\n\r\n```javascript\r\nvar SDK = require('ringcentral');\r\nvar FormData = require('form-data');\r\n\r\nvar sdk = new SDK({\r\n    appKey: 'xxx',\r\n    appSecret: 'xxx',\r\n    server: 'https://platform.ringcentral.com'\r\n});\r\n\r\nvar formData = new FormData();\r\n\r\nvar body = {\r\n    to: [{phoneNumber: '16508370092'}],\r\n    faxResolution: 'High'\r\n};\r\n\r\nformData.append('json', new Buffer(JSON.stringify(body)), {filename: 'request.json', contentType: 'application/json'});\r\n\r\nformData.append('attachment', new Buffer('some plain text'), {filename: 'text.txt', contentType: 'text/plain'});\r\n\r\nformData.append('attachment', require('fs').createReadStream('/Users/tyler.liu/Desktop/test.pdf'));\r\n\r\nconsole.log('sending fax...');\r\n\r\nsdk.platform()\r\n    .login({\r\n        username: 'xxx',\r\n        extension: null,\r\n        password: 'xxx',\r\n    })\r\n    .then(function(response) {\r\n\r\n        console.log('Login successful');\r\n\r\n        return sdk.platform()\r\n            .post('/account/~/extension/~/fax', formData)\r\n            .then(function() {\r\n                console.log('fax sent');\r\n            });\r\n\r\n    })\r\n    .catch(function(e) {\r\n        console.error(e.apiResponse.request());\r\n        console.error(e.stack);\r\n    });\r\n```\r\n\r\n\r\nThe code above fails with the following error message:\r\n\r\n```\r\nsending fax...\r\nLogin successful\r\nBody {\r\n  method: 'POST',\r\n  headers: \r\n   Headers {\r\n     _headers: \r\n      { accept: [Object],\r\n        'x-user-agent': [Object],\r\n        'client-id': [Object],\r\n        authorization: [Object] } },\r\n  url: 'https://platform.ringcentral.com/restapi/v1.0/account/~/extension/~/fax',\r\n  follow: 20,\r\n  compress: true,\r\n  counter: 0,\r\n  agent: undefined,\r\n  body: \r\n   FormData {\r\n     _overheadLength: 477,\r\n     _valueLength: 76,\r\n     _lengthRetrievers: [ [Function] ],\r\n     writable: false,\r\n     readable: true,\r\n     dataSize: 0,\r\n     maxDataSize: 2097152,\r\n     pauseStreams: true,\r\n     _released: false,\r\n     _streams: \r\n      [ '----------------------------471797134592888957481870\\r\\nContent-Disposition: form-data; name=\"json\"; filename=\"request.json\"\\r\\nContent-Type: application/json\\r\\n\\r\\n',\r\n        <Buffer 7b 22 74 6f 22 3a 5b 7b 22 70 68 6f 6e 65 4e 75 6d 62 65 72 22 3a 22 31 36 35 30 38 33 37 30 30 39 32 22 7d 5d 2c 22 66 61 78 52 65 73 6f 6c 75 74 69 ... >,\r\n        [Function: bound ],\r\n        '----------------------------471797134592888957481870\\r\\nContent-Disposition: form-data; name=\"attachment\"; filename=\"text.txt\"\\r\\nContent-Type: text/plain\\r\\n\\r\\n',\r\n        <Buffer 73 6f 6d 65 20 70 6c 61 69 6e 20 74 65 78 74>,\r\n        [Function: bound ],\r\n        '----------------------------471797134592888957481870\\r\\nContent-Disposition: form-data; name=\"attachment\"; filename=\"test.pdf\"\\r\\nContent-Type: application/pdf\\r\\n\\r\\n',\r\n        [Object],\r\n        [Function: bound ] ],\r\n     _currentStream: null,\r\n     _boundary: '--------------------------471797134592888957481870',\r\n     error: \r\n      { [Error: Cannot calculate proper length in synchronous way.]\r\n        apiResponse: [Object],\r\n        originalMessage: 'Cannot calculate proper length in synchronous way.' } },\r\n  bodyUsed: false,\r\n  size: 0,\r\n  timeout: 0,\r\n  _raw: [],\r\n  _abort: false,\r\n  protocol: 'https:',\r\n  hostname: 'platform.ringcentral.com',\r\n  port: null,\r\n  path: '/restapi/v1.0/account/~/extension/~/fax',\r\n  auth: null,\r\n  originalBody: \r\n   FormData {\r\n     _overheadLength: 477,\r\n     _valueLength: 76,\r\n     _lengthRetrievers: [ [Function] ],\r\n     writable: false,\r\n     readable: true,\r\n     dataSize: 0,\r\n     maxDataSize: 2097152,\r\n     pauseStreams: true,\r\n     _released: false,\r\n     _streams: \r\n      [ '----------------------------471797134592888957481870\\r\\nContent-Disposition: form-data; name=\"json\"; filename=\"request.json\"\\r\\nContent-Type: application/json\\r\\n\\r\\n',\r\n        <Buffer 7b 22 74 6f 22 3a 5b 7b 22 70 68 6f 6e 65 4e 75 6d 62 65 72 22 3a 22 31 36 35 30 38 33 37 30 30 39 32 22 7d 5d 2c 22 66 61 78 52 65 73 6f 6c 75 74 69 ... >,\r\n        [Function: bound ],\r\n        '----------------------------471797134592888957481870\\r\\nContent-Disposition: form-data; name=\"attachment\"; filename=\"text.txt\"\\r\\nContent-Type: text/plain\\r\\n\\r\\n',\r\n        <Buffer 73 6f 6d 65 20 70 6c 61 69 6e 20 74 65 78 74>,\r\n        [Function: bound ],\r\n        '----------------------------471797134592888957481870\\r\\nContent-Disposition: form-data; name=\"attachment\"; filename=\"test.pdf\"\\r\\nContent-Type: application/pdf\\r\\n\\r\\n',\r\n        [Object],\r\n        [Function: bound ] ],\r\n     _currentStream: null,\r\n     _boundary: '--------------------------471797134592888957481870',\r\n     error: \r\n      { [Error: Cannot calculate proper length in synchronous way.]\r\n        apiResponse: [Object],\r\n        originalMessage: 'Cannot calculate proper length in synchronous way.' } } }\r\nError: Cannot calculate proper length in synchronous way.\r\n    at FormData.getLengthSync (/Users/tyler.liu/src/js/rc-test/node_modules/form-data/lib/form_data.js:316:17)\r\n    at /Users/tyler.liu/src/js/rc-test/node_modules/node-fetch/index.js:95:48\r\n    at lib$es6$promise$$internal$$initializePromise (/Users/tyler.liu/src/js/rc-test/node_modules/es6-promise/dist/es6-promise.js:408:9)\r\n    at new lib$es6$promise$promise$$Promise (/Users/tyler.liu/src/js/rc-test/node_modules/es6-promise/dist/es6-promise.js:581:60)\r\n    at new Fetch (/Users/tyler.liu/src/js/rc-test/node_modules/node-fetch/index.js:48:9)\r\n    at Fetch (/Users/tyler.liu/src/js/rc-test/node_modules/node-fetch/index.js:36:10)\r\n    at Client._callee2$ (/Users/tyler.liu/src/js/rc-test/node_modules/ringcentral/build/ringcentral.js:1492:53)\r\n    at tryCatch (/Users/tyler.liu/src/js/rc-test/node_modules/ringcentral/build/ringcentral.js:125:40)\r\n    at GeneratorFunctionPrototype.invoke [as _invoke] (/Users/tyler.liu/src/js/rc-test/node_modules/ringcentral/build/ringcentral.js:393:22)\r\n    at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/Users/tyler.liu/src/js/rc-test/node_modules/ringcentral/build/ringcentral.js:158:21)\r\n```\r\n\r\nThere was no problem when I removed this line from source code: `formData.append('attachment', require('fs').createReadStream('/Users/tyler.liu/Desktop/test.pdf'));`  \r\n\r\nFrom the error message, it seems that the code/library failed to calculate the file length.\r\n\r\nLet me do more investigation and get back to this issue later.  By the way, C# SDK works without problem, let me compare the code."},"comment":{"url":"https://api.github.com/repos/ringcentral/ringcentral-js/issues/comments/202197635","html_url":"https://github.com/ringcentral/ringcentral-js/issues/15#issuecomment-202197635","issue_url":"https://api.github.com/repos/ringcentral/ringcentral-js/issues/15","id":202197635,"user":{"login":"tylerlong","id":733544,"avatar_url":"https://avatars.githubusercontent.com/u/733544?v=3","gravatar_id":"","url":"https://api.github.com/users/tylerlong","html_url":"https://github.com/tylerlong","followers_url":"https://api.github.com/users/tylerlong/followers","following_url":"https://api.github.com/users/tylerlong/following{/other_user}","gists_url":"https://api.github.com/users/tylerlong/gists{/gist_id}","starred_url":"https://api.github.com/users/tylerlong/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tylerlong/subscriptions","organizations_url":"https://api.github.com/users/tylerlong/orgs","repos_url":"https://api.github.com/users/tylerlong/repos","events_url":"https://api.github.com/users/tylerlong/events{/privacy}","received_events_url":"https://api.github.com/users/tylerlong/received_events","type":"User","site_admin":false},"created_at":"2016-03-28T02:29:49Z","updated_at":"2016-03-28T02:29:49Z","body":"I've found a solution / workaround:\r\n\r\nReplace the following code:\r\n\r\n```javascript\r\nformData.append('attachment', require('fs').createReadStream('/Users/tyler.liu/Desktop/test.pdf'));\r\n```\r\n\r\nwith\r\n\r\n```javascript\r\nformData.append('attachment', fs.createReadStream('/Users/tyler.liu/Desktop/test.pdf'),\r\n  {filename: 'test.pdf', contentType: 'application/pdf', knownLength: fs.statSync('/Users/tyler.liu/Desktop/test.pdf')['size'] });\r\n```\r\n\r\nI think we should either update the library itself or update the readme documentation."}}