How to send a buffer in form-data to SignServer?

2020-07-18 09:43发布

问题:

I have a file in memory (in a buffer), it doesn't exist on the file system (so I can't just stream that).

I'm trying to send it to SignServer using HTTP.

Here's how I try to do it:

var formdata = require('form-data'); var form = new formdata();

form.append('workerName', 'PDFSigner');
form.append('data', file_buffer);
// or
// escape(file_buffer.toString('binary'))
// or
// file_buffer.toString('binary') (without escaping)

var request = form.submit('http://localhost:8080/signserver/process', function(err, res) {});

When I try appending file_buffer SignServer says that data is empty:

Status 400 - Missing file content in upload

When I try appending escape(file_buffer.toString('binary')) (as suggested in How do I send a buffer in an HTTP request?) it's the same story.

When I try appending file_buffer.toString('binary') node.js crashes saying:

node: ../src/stream_base.cc:157 int node::StreamBase::Writev(const v8::FunctionCallbackInfo&): Assertion `(offset) <= (storage_size)' failed.

Aborted (core dumped)

How do I correctly send the file (buffer) through HTTP (multipart/form-data) in Node.JS?

回答1:

You explicitly need to set a filename for the data field, otherwise the buffer isn't uploaded as a file:

form.append('data', file_buffer, { filename : 'document.pdf' });

This is documented (albeit not very clearly) here: https://github.com/form-data/form-data#alternative-submission-methods (scroll down to the fourth example).