4

I am hitting a get api in react similar to http://www.orimi.com/pdf-test.pdf which needs some secret information which is available only at middleware written in node. I want to open the pdf on client side(browser). So I am hitting the proxy get api which will hit the middleware and middleware will hit the backend server, but I am getting unable to open document and blank pdf. Can anyone tell me what is wrong with this code?

fetch(pdfApiMiddlewareUrl, {

                    method: "GET",

                    headers: {
                        "Content-Type": "application/pdf",
                        'Content-Disposition': 'inline; filename=your_file_name'
                    },
                    responseType : 'blob'
                    })
                    .then(res => res.blob())
                    .then(response => {
                            var blobUrl = URL.createObjectURL(response);
                            window.open(blobUrl);
                    })
                    .catch(error => {
                        console.log("HEREEEEEEEE");
                        console.log(error);
                    });

MIDDLEWARE CODE:

var urlEndPointsToHit = decodeURIComponent(req.query.urlToHit);
var url = urlEndPointsToHit+'?secret='+secretInfo;
var options;
options = {
  url: url,
  method: 'GET',
  headers: {
     'Content-type': 'application/pdf'
   },
};
  if(options) {
    options.qs = req.query || {};
  }

request(options, function(err, resp, body) {
  req.locals = body;
  res.setHeader('Content-Type', 'application/pdf');
  res.setHeader('Cache-Control', 'no-cache');                  
  next();
});
Shihab
  • 2,641
  • 3
  • 21
  • 29

0 Answers0