I am using Express with Node.
I am attempting to send a PDF file as a response.
This PDF file is first retrieved from another server using http.request()
after which I wish to pipe it to the response.
That is, when the browser makes a request for the route '/PDF
, I carry out the following:
app.get('/PDF', function (req, res) { var options = { host: host, path: path, method: 'GET', headers: {} };
var requesting = http.request(options, function (response) {
var body = '';
response.setEncoding('utf8');
response.on('data', function (chunk) {
body += chunk;
});
response.on('end', function () {
var data = null;
if (body) {
try {
data = JSON.parse(body);
}
catch (err) {
data = body;
}
}
res.sendfile(data);
});
});
requesting.end();
});
However, an error is thrown at res.sendfile(data)
:
Error: Bad Request
at SendStream.error (redacted/send.js:145:16)
at SendStream.pipe (redacted/send.js:298:31)
at ServerResponse.res.sendfile (redacted/response.js:339:8)
at IncomingMessage.<anonymous> (redacted/api.js:289:17)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
How do I pass the HTTP GET request body (with content type 'application/pdf') to the browser?