Thursday, 11 May 2017

How do I send a buffer in an HTTP request?

I have a file in memory (buffer) - there is no file on the file system. I want to send that buffer to another server that talks HTTP.

For example, API A makes a file in memory, API B manipulates such files, and responds with a new buffer. My API takes the file from A and feeds it to B. (API B is SignServer)

I tried sending the file to API B in multiple ways, but it keeps responding with status 400 (missing field 'data' in request).


What I tried:

var http = require('http');
var querystring = require('querystring');

var data = querystring.stringify({
    workerName: 'PDFSigner',
    data: file_buffer
});

var request = new http.ClientRequest({
    hostname: 'localhost',
    port: 8080,
    path: '/signserver/process',
    method: 'GET',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        // I also tried 'multipart/form-data'
        'Content-Length': Buffer.byteLength(data)
    }
});

request.write(data);
request.end();

I tried printing data, and it showed:

workerName=PDFSigner&data=

Which is bad because data wasn't set to file_buffer. I tried printing file_buffer, and it does have content (not null, not undefined, actually has bytes inside).

I tried doing the same thing with the request module and it didn't work either.

Note that SignServer isn't written in Node nor JavaScript. It's a Java application, so it probably doesn't work with json (which is why I'm trying to do it with querystring). Yes, I tried sending json.



via Ivan Rubinson

No comments:

Post a Comment