HTTP Post Webresponse gives empty PDF

Mauddib
Member Posts: 269
Hi all,
I am connecting to a Webservice that returns a PDF. The code I am using is below.
The problem is however that the PDF returned appears to be entirely empty!!! However if I build a basic web submit form (also below as code) the PDF returned is perfect.
Therefore I can only surmise that I am damaging the PDF file by saving it to a string and then to a FILESTREAM. Is there a more correct way I should be reading and saving the file that is returned in the response?
When I edit the resulting PDF in NOTEPAD I see the following at the beginning which LOOKS like a correct PDF to me.....
............ should there be some form of conversion I perform before/during saving the PDF???
I am connecting to a Webservice that returns a PDF. The code I am using is below.
HttpClient := HttpClient.HttpClient(); SubmitData := 'partnerid=MY1003'; SubmitData += '&password=mc1222'; SubmitData += '&country=DE'; SubmitData += '&firstname=Me'; SubmitData += '&lastname=McMe'; SubmitData += '&street=Thisstrasse'; SubmitData += '&housenumber=9'; SubmitData += '&zipcode=63741'; SubmitData += '&city=Aschaffenburg'; HttpContent := HttpContent.StringContent(SubmitData, Encoding.UTF8, 'application/x-www-form-urlencoded'); HttpResponseMessage := HttpClient.PostAsync(Uri.Uri('https://api-return-pdf.theirwebsite.com/PDFService/V1/getPDF'), HttpContent).Result; IF (HttpResponseMessage.IsSuccessStatusCode) THEN BEGIN ResponseData := HttpResponseMessage.Content.ReadAsStringAsync().Result; f.CREATEOUTSTREAM(fileoutstream); fileoutstream.WRITETEXT(ResponseData); END;
The problem is however that the PDF returned appears to be entirely empty!!! However if I build a basic web submit form (also below as code) the PDF returned is perfect.
<form action="https://api-return-pdf.theirwebsite.com/PDFService/V1/getPDF" method="POST"> <table> <thead></thead> <tbody> <tr> <td>User, Pass</td> <td><input name="partnerid" type="text"> <input name="password" type="text"> </td> </tr> <tr> <td>Vorname, Nachname</td> <td><input name="firstname" type="text"> <input name="lastname" type="text"> </td> </tr> <tr> <td>Firma:</td> <td><input name="company" type="text"></td> </tr> <tr> <td>Zusatz Adress Info</td> <td> <input name="additionalinfo" type="text"> </td> </tr> <tr> <td>Strasse, Nr.</td> <td><input name="street" type="text"> <input name="housenumber" type="text"> </td> </tr> <tr> <td>PLZ, Stadt, country:</td> <td><input name="zipcode" type="text"> <input name="city" type="text"> <input name="country" type="de"> </td> </tr> <tr> <td>Kundenreferenznummer</td> <td> <input name="kdrefno" type="text"> </td> </tr> - 15 - </tbody> </table> <input type="submit" value="Send" /> <input type="reset" /> </form>
Therefore I can only surmise that I am damaging the PDF file by saving it to a string and then to a FILESTREAM. Is there a more correct way I should be reading and saving the file that is returned in the response?
When I edit the resulting PDF in NOTEPAD I see the following at the beginning which LOOKS like a correct PDF to me.....
%PDF-1.4 %???? 3 0 obj <</Type/XObject/ColorSpace/DeviceGray/Subtype/Image/BitsPerComponent 8/Width 227/Length 30/Height 35/Filter/FlateDecode>>stream x???
............ should there be some form of conversion I perform before/during saving the PDF???
0
Best Answers
-
I believe it maybe because it is Base64 encoded. Try decoding it with something like the following...
Array := Convert.FromBase64String(ResponseData); MemoryStream := MemoryStream.MemoryStream(Array); TempBlob.Blob.CREATEOUTSTREAM(OutStream); MemoryStream.WriteTo(OutStream); TempBlob.Blob.EXPORT('Output.PDF'); VARs as follows... Convert DotNet 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert Array DotNet 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array MemoryStream DotNet 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.MemoryStream OutStream OutStream TempBlob Record TempBlob
P.S. In newer versions of NAV you should use the BlobExport function in codeunit 319 "3-Tier Automation Mgt" to export data from the Blob field5 -
Thanks for that, but alas no
I coded it and ran it and the message I got was:
"A call to FromBase64String failed with this message: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters".5 -
OK It is solved!
And your code above gave me the clue.
Instead of reading it in now with ReadAsStringAsync I used your code above and read ReadAsByteArrayAsync into your ARRAY variable.
I then simply deleted the CONVERT code and did the rest and the file produced by this saved perfectly.
So now I know HOW to do it I would like to know WHY it works. Can someone explain to me the difference between reading the response into text and streaming it out to a file.... and streaming it to Blob and exporting a file?
I guess something somewhere is encoded differently? But what, how and why?5
Answers
-
I believe it maybe because it is Base64 encoded. Try decoding it with something like the following...
Array := Convert.FromBase64String(ResponseData); MemoryStream := MemoryStream.MemoryStream(Array); TempBlob.Blob.CREATEOUTSTREAM(OutStream); MemoryStream.WriteTo(OutStream); TempBlob.Blob.EXPORT('Output.PDF'); VARs as follows... Convert DotNet 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert Array DotNet 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array MemoryStream DotNet 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.MemoryStream OutStream OutStream TempBlob Record TempBlob
P.S. In newer versions of NAV you should use the BlobExport function in codeunit 319 "3-Tier Automation Mgt" to export data from the Blob field5 -
Thanks for that, but alas no
I coded it and ran it and the message I got was:
"A call to FromBase64String failed with this message: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters".5 -
OK It is solved!
And your code above gave me the clue.
Instead of reading it in now with ReadAsStringAsync I used your code above and read ReadAsByteArrayAsync into your ARRAY variable.
I then simply deleted the CONVERT code and did the rest and the file produced by this saved perfectly.
So now I know HOW to do it I would like to know WHY it works. Can someone explain to me the difference between reading the response into text and streaming it out to a file.... and streaming it to Blob and exporting a file?
I guess something somewhere is encoded differently? But what, how and why?5
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions