1. vignesh ramkumar
  2. PowerBuilder
  3. Tuesday, 27 October 2020 09:38 AM UTC

Hi Experts,

 

We have a web portal where we have an api which is called from the PowerBuilder application it will gives as a pdf file as out put. So We are using HTTPClient to achieve this but we are getting a response from web api but we stuck on where we dont know how we will convert that response into pdf file or  how directly we can download that pdf file.

 

Code in PB:

Integer li_rc,li_FileNum
String ls_ReturnJson
HttpClient lnv_HttpClient

lnv_HttpClient = Create HttpClient

String ls_json = '{"BranchId":1, "RTFFileName":" test", "SpName": "test"}'


lnv_HttpClient.SetRequestHeader("Content-Type", "application/json;charset=UTF-8")
// Content-Length header set by SendRequest


li_rc = lnv_HttpClient.SendRequest("POST", "our-url", ls_json, EncodingUTF8!)

if li_rc = 1 and lnv_HttpClient.GetResponseStatusCode() = 200 then
lnv_HttpClient.GetResponseBody(ls_ReturnJson)
end if


Messagebox('',ls_ReturnJson)

 

Response we are getting as below:


%PDF-1.7

%����

1 0 obj

<</Type/Catalog/Pages 2 0 R/Lang(en-IN) /StructTreeRoot 20 0 R/MarkInfo<</Marked true>>/Metadata 75 0 R/ViewerPreferences 76 0 R>>

endobj

2 0 obj

<</Type/Pages/Count 1/Kids[ 3 0 R] >>

endobj

3 0 obj

<</Type/Page/Parent 2 0 R/Resources<</Font<</F1 5 0 R/F2 9 0 R/F3 11 0 R/F4 13 0 R>>/ExtGState<</GS7 7 0 R/GS8 8 0 R>>/XObject<</Image18 18 0 R>>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 612 792] /Contents 4 0 R/Group<</Type/Group/S/Transparency/CS/DeviceRGB>>/Tabs/S/StructParents 0>>

endobj

4 0 obj

<</Filter/FlateDecode/Length 2635>>

stream

x��\[O�H~G�?ڗd4)\�\���v����C�B�8���`�ſ�sNٹ�ir)�H@\��|�~)�O��QrS�7o��"��C68���^�<�����]�wyvx�|]���i2L'GG������ x��Y!Y�"�m�d�t�_X��wr��w�N0!x���hO�}�HH͌y������a�O�'�����z��7��Wv���������^'
7����p�'yQ��9}��ONU��%�ve�^<&��vʂ�Iv�:i��r���l@��KŢ(��U�M��.�� h$�1�&�<�Kh�(

so we are stuck after this. I need to show the pdf file from this response.

Accepted Answer
René Ullrich Accepted Answer Pending Moderation
  1. Tuesday, 27 October 2020 09:44 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi Vignesh,

Try to use function GetResponseBody with a blob variable (instead of a string variable) to store the returned PDF. Than you can write the blob to a file.

HTH,

René

Comment
There are no comments made yet.
vignesh ramkumar Accepted Answer Pending Moderation
  1. Wednesday, 28 October 2020 04:03 AM UTC
  2. PowerBuilder
  3. # 1

Thanks much guys for your thought on this discussion its really helped me a lot smile

 

Armeen Mazda @Appeon  Daryl Foster mike S

     

 

Comment
There are no comments made yet.
vignesh ramkumar Accepted Answer Pending Moderation
  1. Wednesday, 28 October 2020 03:59 AM UTC
  2. PowerBuilder
  3. # 2

Thanks much René Ullrich it worked I had declared the blob variable in that variable i store the response got from the api and then i write into pdf file. coolsmile. Appreciate your help.

Comment
There are no comments made yet.
Daryl Foster Accepted Answer Pending Moderation
  1. Wednesday, 28 October 2020 03:49 AM UTC
  2. PowerBuilder
  3. # 3

I agree with René below, in the example above the pdf isn't being returned as base64 so you'll need to save it to a blob before saving to a file.  There is some extra stuff you can do with the response headers to make sure that it is a pdf, but here is an basic example:

blob blb_response
string ls_filename
integer li_File
integer li_rc

li_rc = lnv_HttpClient.GetResponseBody(blb_response)
if li_rc = 1 then
    // Get the filename somehow, I've just hardcoded it here, but you might be able
    // to get the filename from the response headers
    ls_filename = 'file.pdf'
    li_File = FileOpen(ls_filename, StreamMode!, Write!, Shared!, Replace!)
    if li_File > 0 then
        FileWriteEx(li_File, blb_response)
        FileClose(li_File)
    else
        MessageBox('Error', 'Error writing to ' + ls_filename + ' [' + string(li_file) + ']')
    end if
end if

 

Comment
There are no comments made yet.
Armeen Mazda @Appeon Accepted Answer Pending Moderation
  1. Tuesday, 27 October 2020 14:42 PM UTC
  2. PowerBuilder
  3. # 4

PDF or any blob for that matter must be Base64 encoded and then decoded.  You cannot simply send blob in JSON as JSON is just string.

I recommend you go through the REST boot camp videos: 

https://www.appeon.com/developers/library/videos/rest-bootcamp.html

https://www.appeon.com/developers/library/videos/enhanced-restclient-powerbuilder-2019.html

Comment
  1. mike S
  2. Tuesday, 27 October 2020 16:38 PM UTC
adding to what armeen posted, the api docs should tell you whether the body has only base64 encoded pdf file, or if there is more to it.



from what you posted it looks like its just the pdf. So (base64) decode it , then write it to a file with the pdf extension.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.