Possible Web Api Report service bug  Topic is solved

Discussions relating to the REST API of Jiwa 7.

Possible Web Api Report service bug

Postby SBarnes » Fri Jul 27, 2018 2:48 pm

Hi Mike,

I am trying to use the report service to return a pdf file but I am getting blank files through the Swagger UI and from code. When I force the Api to launch visual studio and step the code through until the file is produced in the temp directory, Crystal is writing the file correctly but the file returned from ServiceStack is blank from code and SwaggerUI, see the code call below, any ideas?


Code: Select all
            ReportsPDFGETRequest pdfreq = new ReportsPDFGETRequest();
            pdfreq.ReportID = "a78c84d7-ebb6-4ff9-8974-e0f9c3680094";
            pdfreq.ReportParameters = new List<ReportParameter>();
            pdfreq.AsAttachment = true;
            pdfreq.ReportParameters.Add(new ReportParameter() { Name = "Pass_SP_StartingDebtorAccountNo", Value = "CHRI"});
            pdfreq.ReportParameters.Add(new ReportParameter() { Name = "Pass_SP_EndingDebtorAccountNo", Value ="CHRI" });
            pdfreq.ReportParameters.Add(new ReportParameter() { Name = "Pass_SP_AsAtDate", Value = DateTime.Now.ToString("MM/dd/yyyy") });
            var streamstring = client.Get<string>(pdfreq);


           
            byte[] bytes = Encoding.ASCII.GetBytes(streamstring);
           
            System.IO.File.WriteAllBytes("c:\\install\\testrep.pdf", bytes);
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Possible Web Api Report service bug

Postby Mike.Sheen » Fri Jul 27, 2018 3:10 pm

Hi Stuart,

The problem with Swagger-UI downloading corrupt files is a known issue with that version of Swagger-UI - and they've fixed it in a later version, but won't be patching the version ServiceStack currently ships with - so we're waiting for ServiceStack to update their stuff to use the newer Swagger-UI - but ServiceStack have held off on that because the newer Swagger-UI introduces other issues.

I've tested this out and I can get the file using our demo data.

https://api.jiwa.com.au is wired up to our demo data, report ID "931b9c65-ce26-4b2d-b7f4-ed50e1ac9574" is a simple report that can be driven without parameters, and we've got an API key "9vjoF4M5-gYVBWjoFyW9nlQieB9FhxpJkAsRJznB_Ok" you can use to make the request in one step - can you just see if that presents the same problem for you?

Example call in Curl:
Code: Select all
curl -H 'Authorization: Bearer 9vjoF4M5-gYVBWjoFyW9nlQieB9FhxpJkAsRJznB_Ok' -X GET https://api.jiwa.com.au/Reports/931b9c65-ce26-4b2d-b7f4-ed50e1ac9574/PDF/Download
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Possible Web Api Report service bug

Postby SBarnes » Fri Jul 27, 2018 4:27 pm

Hi Mike,

Thanks for the response I am still a bit perplexed as to why the code I used didn't work even if Swagger didn't, is there any code for test cases that tests the api in this area that you could provide so I can company the two and in the mean time I'll try calling the online version of the api and see how it goes?
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Possible Web Api Report service bug

Postby SBarnes » Fri Jul 27, 2018 5:18 pm

Hi Mike

the following code produces a blank pdf as well.

Code: Select all
            JsonServiceClient client2 = GetAPIClient("https://api.jiwa.com.au");

            ReportsPDFGETRequest pdfreq = new ReportsPDFGETRequest();

            pdfreq.ReportID = "931b9c65-ce26-4b2d-b7f4-ed50e1ac9574";
            pdfreq.ReportParameters = new List<ReportParameter>();
            pdfreq.AsAttachment = true;

            var streamstring = client2.Get<string>(pdfreq);



            byte[] bytes = Encoding.ASCII.GetBytes(streamstring);

            System.IO.File.WriteAllBytes("c:\\install\\testrep2.pdf", bytes);

Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Possible Web Api Report service bug

Postby Mike.Sheen » Sat Jul 28, 2018 11:59 am

Hi Stuart,

Thanks for the code - I too am getting a blank PDF.

I didn't read your original post properly and leapt straight to the problem I was familiar with Swagger-UI producing 0 byte files - and in my testing I was getting a 47KB file and didn't actually bother to open it to see it was blank - which is what you actually reported.

I'm looking into it now - should be an easy fix.

Mike
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Possible Web Api Report service bug

Postby SBarnes » Sat Jul 28, 2018 12:10 pm

Hi Mike,

Thanks for the reply particularly on a weekend, if its of any help I have seen similar things on Web API projects with pdf, usually it has had to do with a buffer not being flushed to the end.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Possible Web Api Report service bug

Postby Mike.Sheen » Sat Jul 28, 2018 12:17 pm

SBarnes wrote:if its of any help I have seen similar things on Web API projects with pdf, usually it has had to do with a buffer not being flushed to the end.


Yep, it looks like that could be the case.

Interestingly so far I've found by not using the jsonserviceclient and instead using ServiceStacks string extension methods HTTPUtils, the following works fine - it produces a 49KB pdf (2KB bigger) and is not blank!

Code: Select all
string downloadUrl = "https://api.jiwa.com.au/Reports/931b9c65-ce26-4b2d-b7f4-ed50e1ac9574/PDF/Download?apikey=9vjoF4M5-gYVBWjoFyW9nlQieB9FhxpJkAsRJznB_Ok";
var rawBytes = downloadUrl.GetBytesFromUrl();
System.IO.File.WriteAllBytes("c:\\users\\mikes\\downloads\\testrep2.pdf", rawBytes);
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Possible Web Api Report service bug

Postby SBarnes » Sat Jul 28, 2018 12:29 pm

I am betting in the Web API if you change the code to use a file stream to read the file Crystal creates and put that in a memory stream and flush and close the file stream it will work as that was what I had to do in the other project I worked on and I believe the ServiceStack http result class has a method that will take a stream to return.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Possible Web Api Report service bug  Topic is solved

Postby Mike.Sheen » Sat Jul 28, 2018 12:48 pm

SBarnes wrote:I am betting in the Web API if you change the code to use a file stream to read the file Crystal creates and put that in a memory stream and flush and close the file stream it will work as that was what I had to do in the other project I worked on and I believe the ServiceStack http result class has a method that will take a stream to return.


Ok, I'll look into that, but I found a way to get a valid file using the JsonServiceClient:

Code: Select all
JsonServiceClient client = new JsonServiceClient("https://api.jiwa.com.au");
client.Headers.Add("Authorization", "Bearer 9vjoF4M5-gYVBWjoFyW9nlQieB9FhxpJkAsRJznB_Ok");

ReportsPDFGETRequest pdfreq = new ReportsPDFGETRequest();

pdfreq.ReportID = "931b9c65-ce26-4b2d-b7f4-ed50e1ac9574";
pdfreq.ReportParameters = new List<ReportParameter>();
pdfreq.AsAttachment = true;

var httpResponse = client.Get<System.Net.HttpWebResponse>(pdfreq);
System.IO.File.WriteAllBytes("c:\\users\\mikes\\downloads\\testrep2.pdf", httpResponse.GetResponseStream().ToBytes());


The only difference really to what you had is returning the request into a System.Net.HttpWebResponse instead of a string, and then writing the contents of it's GetResponseStream as a byte array.

I'd like to look at the actual raw responses to see why this works and your method didn't - it doesn't make a whole lot of sense to me.

I hope this is a satisfactory result for you - I'll still see if changing the Service in the API to read the file content into a stream and flushing it changes things - but I think the above is a reasonable workaround for now.


Ok, after digging deeper it's not a server side problem - it's actually by design to handle large files and allow progress indicator type scenarios. The JsonServiceClient uses a stream so you can download chunks and provide the user with a progress indicator type scenario if desired - or like the above code shows, you can just get the whole lot at once. An example of that I found in this StackOverflow answer.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756


Return to REST API

Who is online

Users browsing this forum: Bing [Bot] and 6 guests

cron