Expand my Community achievements bar.

SOLVED

HttpClientRequest not returning the full response

Avatar

Level 2

Hello,

My goal is to have a webform with a link for the client to download a PDF, this PDF should come from a HTTP request from a server in our network and I will display it via webform to the public.

I'm not able to receive the full response for a PDF on an HttpClientRequest, it seems that everytime the pdf is incomplete, if I try with a website URL the response is longer and ok, but never with PDF.

I've try all kinds of hearders but with no success.

Here's a sample of the code:

Code

var url = 'https://www.soundczech.cz/temp/lorem-ipsum.pdf';

var http = new HttpClientRequest(url);

http.method = "GET";

http.header["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36";

http.header["Accept"] = "application/pdf";

http.header["Content-Type"] = "application/pdf;charset=utf-8";

http.header["accept-encoding"] = "gzip, deflate";

http.execute();

logInfo(http.response.body);

pdf = http.response.body;;

The logInfo goes to web@default and the pdf is displayed on webpage.

Thanks for any help

1 Accepted Solution

Avatar

Correct answer by
Level 2

Hello,

I managed to do this in another way (will share how, in case more people need it).

I was using the code in a JavaScript and using a webApp variable to pass the content from the JS to the page item.

This was solved by executing the javascript code in the page itself.

Apparently we can't store the result of a PDF in a var because there are some EOF text that can corrupt the result. So the final code is:

<head>

<%

var url = 'https://www.soundczech.cz/temp/lorem-ipsum.pdf';

var http = new HttpClientRequest(url);

http.method = "GET";

http.execute();

%>

</head>

<body style="" class="">

<p>Stuff:</p>

<a href="data:application/pdf;base64, <%= http.response.body.toBase64() %>" target="_blank" download=""> pdf </a>

This will enable the user to download the PDF.

View solution in original post

7 Replies

Avatar

Level 10

Hi jferreira,

Please may you achieve the same again but with try catch instructions, and share the detailed errors (web.log, ctx in debug mode, etc).

There could be many issues:

  • urlPermission not defined in serverConf (because in your use case, the pdf is not located on Adobe Campaign server but another server)
  • CORS (allow origin) for https link in some case (such as iframe encapsulation, etc)
  • timeout if any firewall is blocking
  • other reasons

You can also use the alternative simple way getUrl() function, but it won't resolve your issue if it is a network issue or security issue...

http://docs.campaign.adobe.com/doc/AC/en/jsapi/f-getUrl.html?hl=geturl

One more thing, if you have access to curl, please run it to check if the access is right.
Or if you have access to system exec command, do the wget equivalent:
http://docs.campaign.adobe.com/doc/AC/en/jsapi/f-execCommand.html?hl=wget

Regards
J-Serge

Avatar

Level 2

Hello,

Thanks for the tips.

I'm now testing on a workflow because it's faster. The log I see is something like this:

The feeeling I have is that the response it not complete or there's something limiting the response. This was obtained with the getUrl() function, seems the output it's the same.

About permissions, I would say it's not the case because I can receive partial answer, so it connects successfully.

Although if you think it's an issue on serverConf, maybe I can give it a try.

Thanks

Avatar

Level 10

Hi J Ferreira,

You are right, because the transfer starts successfully even though not complete, it is not a question of permissions/network access, sorry for my bad suggestions in your case.

In your workflow, did you put try/catch as well?

To my mind, it should be a question of some specific characters treated as end of file/stopping transmission or something like that, because pdf is binary file, not text file.
The same for logInfo() perhaps broken as well?
Not sure, sorry.

Please read in JSAPI documentation the memoryBuffer function and manipulation for such binary contents, probably you would save the content in a file (without logInfo) and then compare the saved file with the original file.

But I wonder, why must be read the pdf content and display it in a webApp (I guess with an iframe)? Won't it be possible to use an iframe with the file with argument and the standard Adobe Reader web object doing the job for displaying the content?

Regards
JS

Avatar

Level 2

I tried with a try catch but the end result is the same.

I have the same feeling, there should be a set of chars that ends the stream, be it in logInfo or displaying it in the page.

I think I'll have to try another approach.

I must read the PDF because we have it in a webserver in the network that is not public, so I need to grab the PDF and make it public.

Thank you for the help

Avatar

Level 10

Ok so if it is the only reason, you should get the file (urlGet), saving it "locally" on the Adobe Campaign res specific sub-folder of your choice, then you expose this file to the webform link/button.

Using MD5 for naming the "local" file and so reduce security issue for such public folder.
Then cleansing the subfolder either immediately or not.

Perhaps someone else could give other advices for doing the same easily.


Regards
JS

Avatar

Level 2

This can be a good solution!

Can you provide a quick example?

Thank you so much

Avatar

Correct answer by
Level 2

Hello,

I managed to do this in another way (will share how, in case more people need it).

I was using the code in a JavaScript and using a webApp variable to pass the content from the JS to the page item.

This was solved by executing the javascript code in the page itself.

Apparently we can't store the result of a PDF in a var because there are some EOF text that can corrupt the result. So the final code is:

<head>

<%

var url = 'https://www.soundczech.cz/temp/lorem-ipsum.pdf';

var http = new HttpClientRequest(url);

http.method = "GET";

http.execute();

%>

</head>

<body style="" class="">

<p>Stuff:</p>

<a href="data:application/pdf;base64, <%= http.response.body.toBase64() %>" target="_blank" download=""> pdf </a>

This will enable the user to download the PDF.