Skip to main content
Question

Box Node SDK - upload issue with non-file read stream

  • May 22, 2025
  • 3 replies
  • 40 views

Forum|alt.badge.img

My nodejs app has a client authenticated as an enterprise user. It works fine if I try to upload a file as below:

var fs = require("fs");
var stream = fs.createReadStream("z.txt");

boxClient.files
        .uploadFile(folderId, filename, stream)
        .catch(reason => console.log("Upload file error", reason));
 

 

However, I don't have a file but a stream. I tried with a simple readable stream and it fails:

const Readable = require("stream").Readable;
const s = new Readable();
s.push("testing");
s.push(null);

boxClient.files
        .uploadFile(folderId, filename, s)
        .catch(reason => console.log("Upload file error", reason));

 The error I get is:

"Error: Unexpected API Response [400 Bad Request] bad_request - Stream ended unexpectedly"

 

Is the SDK treating file streams differently or am I missing something?

3 replies

Forum|alt.badge.img

 In order to construct the API request, the internal networking library the SDK uses requires the stream to supply the amount of data it will provide (measured in bytes).  The allows the library to correctly calculate the `Content-Length` header for the request.  You can read more about this in a previously-reported issue on GitHub, which includes a helpful workaround for custom streams: https://github.com/box/box-node-sdk/issues/143


Forum|alt.badge.img

 can you provide any guidance on _how_ you might indicate size?

 

For example, I have 

 

await client.files
.uploadFile(FOLDER_ID, fileName, fileToUpload, {size})

 

where size is the `size` in bytes of the `fileToUpload`
 
I've looked at the API docs (https://developer.box.com/reference/post-files-content) and don't see anything there. 
 
Thanks!

Forum|alt.badge.img

Update -- you can't: https://github.com/box/box-node-sdk/issues/144

 

size is only available on the Chunked Upload