I have a node service that fetches a pdf from an API and serves that pdf.
When I curl or directly open the API, I do see the correct pdf.
But when I serve it from my Node app, I get an empty pdf.
Here's the section of my code that does the pdf render.
} else if (options.type === 'pdf') { res.writeHead(200, {'content-type' : 'application/pdf', 'content-disposition': 'attachment; filename=invoice.pdf'}); res.end(data.invoice); I've console.log'ed data.invoice to know it's the right stuff.
typeof(data.invoice) gives string; but I've also tried res.end(new Buffer(data.invoice)); which didn't work either.
Here's the section of my code that fetches the data
var http_options = { method : options.method , host : Config.API.host , path : options.path , port : Config.API.port , headers : options.headers }; var req = http.request(http_options, function (response) { var raw_response = ""; response.on('data', function (response_data) { raw_response += response_data.toString(); }); response.on('end', function () { if (response.statusCode !== 200) { cb(raw_response); } else { cb(false, raw_response); } }); }); req.setTimeout(timeout, function () { req.abort(); cb("API connection timed out"); }); req.on('error', function (error) { cb("API error while requesting for " + options.path + '\n' + error + '\n' + "http options: " + JSON.stringify(http_options) }); req.end();
Content-Type? Because you wrote lowercase "c" and "t"