8

I am using swagger-UI 2.1.3 for API documentation and in backend, I am using spring-webmvc .

I have one API which returns a pdf file, it works fine if I type the URL in browser (it popups a download and downloaded file works fine)

But the same api won't works in swagger ui, it gives me a download link after clicking on "try out", and that link downloads a file, but that file shows me blank pdf pages (corrupted pdf file) .

The response headers are following

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: public
Expires: 0
Access-Control-Allow-Origin: *
Content-Description: File Transfer
Content-Transfer-Encoding: binary
Transfer-Encoding: chunked
Content-Disposition: attachment; filename="example.pdf"
Access-Control-Expose-Headers: Content-Description,Content-Disposition,location
Content-Type: application/pdf
Content-Length: 268288
Date: Mon, 04 Jan 2016 12:18:16 GMT

Any solution around this ?

Addition Info:

This questions seems similar - AngularJS: Display blob (.pdf) in an angular app

There they are saying that set responseType to arraybuffer in xhr, But i think that swaggar will take care of that (maybe i need to set some configuration??)

Community
  • 1
  • 1
vivex
  • 2,496
  • 1
  • 25
  • 30
  • Is Content-Length same as pdf file size? – Piyush Jan 04 '16 at 13:37
  • yes file length is same as pdf file size – vivex Jan 05 '16 at 11:45
  • I had the same problem. I added `produces = { "application/pdf"}` in `@RequestMapping` and then i chose "application/pdf" as response type in swagger-ui and pdf file downloaded correcty. If it will not work, try to change produces media type to application/octet-stream and manually add extension to file after downlad. Also, to accurately limit the problem in swagger-ui try to meet your request in postman – Mikhail Nov 08 '17 at 16:53

0 Answers0