• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Rest Controller Response has Garbage while downloading a excel report

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm working on generating few reports for my application. We used spring boot to write the REST API. I wanted to generate and download the report (I don't want to hard code the download location instead I want to download the report to the user's downloads folder) to the user's machine, also to send a success JSON response to the client.

The report is generation is successful, however.. when I try to download the report and send the successful JSON response to the client, I'm having issue.

When I do this, the report is generated and the JSON response shows successful code.



However, when I do this, the report is not downloaded instead, its trying to print on the postman console and its all garbage.



Can you please suggest, what went wrong?
 
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

santosh batta wrote:the report is not downloaded instead, its trying to print on the postman console and its all garbage.


This is normal Postman behaviour when it is unable to render the content.



Click on the Download button, specify a location to save your content, and view it with an application which can work the content type.
 
Ron McLeod
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option is rather than using Send, use Send and Download.

 
Bartender
Posts: 20940
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot force a file to be downloaded onto a client's computer from a web server. That would be a horrible security issue, with rogue servers infecting clients all over the place.

A ReST client, on the other hand, would be able to request and download data that the ReST client could then store as a file.

Note the key differences here. One approach would have the server in control, the other has the client in control. The client is expected to know what it wants.

Because a web server is not a file server, what you actually get back from a ReST request is not actually a file. It's an HTTP response data stream, including headers, cookies and the like as well as the data body that will be stored as a file. Among the recommended headers are the Content-Type, which should have a value of application/vnd.ms-excel, the Content-Disposition, which should be something like attachment; filename="filename.jpg", and (optional, but recommended) Content-Length which is the length in bytes of the payload.

The filename part of content-disposition is the recommended name that the file should be saved under. Ideally, it should by only a name, since you cannot assume the directory structure of the client's machine. But in any event, it's only a recommendation. When using a browser as a web client with an attachment content-disposition, the browser will present a File Save dialog and (usually) the recommended filename will be pre-loaded into that dialog. To repeat, the server cannot force the file to be saved. Instead, by presenting this dialog. the user is given the choice on whether and where to save the incoming data into a file created by the client machine.

An automated ReST client running stand-alone doesn't need this dialog - or any GUI interaction - since (hopefully) the ReST client is an application installed with the approval of the user. Who knows what it's doing*. So the automated client can process the headers any way it likes and create/update/delete locally-accessible files to its heart's content, subject to local filesystem permissions.

----
* Roughly speaking. Actual users, of course, haven't the slighest clue and figure it's all the work of magical elves. But someone who allegedly knows better directed it as part of the larger plan.
 
Sheriff
Posts: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

santosh batta wrote:When I do this, the report is generated and the JSON response shows successful code.


The report is generated, but on the server, not on the client's machine. It just happens to work for you because the server is on your own machine. Try calling the endpoint from a different machine, and you'll notice the file will not be saved there.
 
santosh batta
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

santosh batta wrote:the report is not downloaded instead, its trying to print on the postman console and its all garbage.


This is normal Postman behaviour when it is unable to render the content.



Click on the Download button, specify a location to save your content, and view it with an application which can work the content type.



Yup, it worked. Thanks much.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!