Writing pdf file is giving file type plain text is not supported

This is my code for writing a pdf file that the server is sending. When I use a third party tool and make a call, it's sending the pdf which is fine and can be opened.

But when a pdf is generated by following code, the pdf cannot be viewed and is giving file type plain text. I'm using nodejs.

request(options, function(err, res, body) {

   body.on('data', function(data) {
        file.writeHead(200, {'Content-Type': 'application/pdf'});
        file.write(data);
   }).on('end', function() {
        file.end();
   });
});