Hi Vyasavardhan,
I agree with Hermanni that if all you want to do is read the file from the local file system and place it into a DataPower context, fetch with the binary output type is a lot simpler than writing your own code. I do have some questions about your code though. The urlopen.open and fs.readFile functions are effectively doing the same thing, they are reading the file from the file system and providing you a variable that will contain the file contents. It appears you are using urlopen.open simply to determine if the file exists by getting a status code of 200, but assuming you do, you can then do a response.readAsBuffer function within the urlopen.open to use the response object to get the payload. The urlopen.open has already read the payload, but you must access it with a readAsxxxx function. In fact, https://www.ibm.com/docs/en/datapower-gateway/10.0.1?topic=apis-urlopen-module#urlopen.opentoopenlocalfiles has an example of exactly this. By not doing a readAsBuffer function on the response, you're probably seeing log messages indicating that a payload was implicitly discarded and forcing DataPower to do a lot of unnecessary cleanup for you.
You could also simply use the fs.exists() function to determine if the file exists, and if so do the fs.readFile within its async function. In fact https://www.ibm.com/docs/en/datapower-gateway/10.0.1?topic=apis-fs-module#fs.read has that exact example.
Finally, your first example has the correct session.output.write(data). The session.output will take the data in the argument and will write it to the output context specified on the GatewayScript action that references your .js. The second example with session.output.write(data+'.pdf'); is taking your pdf data and concatenating a string to it which would make the data written to your output context to have non pdf data.
From a DataPower perspective, what are you trying to accomplish? Either using the fetch action, the urlopen.open with a subsequent response.readAsBuffer, or the fs.readFile function, the latter two writing the returned file content to a DataPower context, are you wanting a browser, doing a HTTP GET to your DataPower service, to return your pdf as a payload that the browser would automatically show in the browser? I'm not sure without some research about what response headers you'd need to provide, but if you're providing the payload correctly, then you'll need response headers, for example you would need at a minimum a pdf content type (application/pdf) in the transaction's response headers. After deciding upon the mechanism of getting the file content into a DataPower context, you can set the response payload content type using a GatewayScript action, transformation action (xslt), or even a set variable action. In GatewayScript
var hm = require('header-metadata');
hm.response.set('Content-Type', 'application/pdf');
Best Regards,
Steve
------------------------------
Steve Linn
Senior Consulting I/T Specialist
IBM
------------------------------
Original Message:
Sent: Mon November 14, 2022 01:52 AM
From: Vyasavardhan Ramagiri
Subject: How can i open a pdf file which is stored in local diretory of datapower through get method?
Actually I've implemented 2 cases.
case 1 :
by using the following gatewayscript code i can able to download a file but it is downloaded by .file extension but when i renamed it to .pdf then my pdf file is opening.
the code is :
var urlopen = require('urlopen');
var fs = require('fs');
urlopen.open("local:///api.pdf", function (error, response) {
if (error) {
session.output.write("openCallback error: " + error.errorMessage+"\n");
}
else {
if (response.statusCode == 200) {
// You have a 200, so you can read the file
fs.readFile("local:///api.pdf", function(error,data) {
session.output.write(data);
});
}
}
});
case 2 :
by using the following gatewayscript code i can able to open a pdf file when the url is placed in the browser but it is showing "failed to open pdf document".
a response attachment is added for your understanding.
var urlopen = require('urlopen');
var fs = require('fs');
urlopen.open("local:///api.pdf", function (error, response) {
if (error) {
session.output.write("openCallback error: " + error.errorMessage+"\n");
}
else {
if (response.statusCode == 200) {
// You have a 200, so you can read the file
fs.readFile("local:///api.pdf", function(error,data) {
session.output.write(data+'.pdf');
});
}
}
});
so now the issue is either i want to load the pdf in browser or i need a file with ".pdf" extension should be downloaded.
please can anyone will help me regarding this.
Thanks in Advance....!
------------------------------
Thanks and Regards,
Vyasavardhan Ramagiri
------------------------------