Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue in downloading/viewing the PDF content with content type "application/pdf" and "application/octet-stream" in Swagger 3.0 #4250

Closed
balaji-tech87 opened this issue Feb 23, 2018 · 5 comments

Comments

@balaji-tech87
Copy link

Q A
Bug or feature request? Bug
Which Swagger/OpenAPI version?
Which Swagger-UI version? 3.X
How did you install Swagger-UI?
Which browser & version? Chrome
Which operating system? Windows

Demonstration API definition

Case 1:
responses:
'200':
description: "".
content:
application/pdf:
schema:
type: string
format: binary

Case 2:
responses:
'200':
description: "".
content:
application/octet-stream:
schema:
type: string
format: binary

Configuration (browser query string, constructor, config.yaml)

Case1:
curl -X GET "http://localhost:8080/rest/*****true" -H "accept: application/pdf"

Case2:
curl -X GET "http://localhost:8080/rest/*****true" -H "accept: application/octet-stream"

Expected Behavior

It should either display the PDF content in the browser or it should provide link to download the file.
And with the same curl command returns the download prompt for pdf file, while trying in postman tool.

Current Behavior

Case 1:
It throws Not acceptable error - 406.

Case 2:
It throws 200 - Unknown response type.

Possible Solution

Context

Writing an api to download the PDF content
case-1_error
case-2_error

@webron
Copy link
Contributor

webron commented Feb 23, 2018

You haven't specified which version of swagger-ui.

Also, in this case, without a live server for us to test against, it would be difficult to reproduce.

@hkosova
Copy link
Contributor

hkosova commented Feb 27, 2018

@balaji-tech87 your example works fine for me using Swagger UI 3.11.0 and PDFs on GitHub. The response section displays the "Download file" link.

The issue is likely with your server.


PDF download link

openapi: 3.0.0
info:
  title: PDF test
  version: 0.0.0

servers:
  - url: https://raw.githubusercontent.com

paths:
  /mozilla/pdf.js/master/examples/learning/helloworld.pdf:
    get:
      responses:
        '200':
          description: A PDF file
          content:
            application/pdf:
              schema:
                type: string
                format: binary
            application/octet-stream:
              schema:
                type: string
                format: binary

@shockey
Copy link
Contributor

shockey commented Feb 27, 2018

Hi @balaji-tech87!

I ran @hkosova's example through a web proxy that modified GitHub's Content-Type: application/octet-stream to be Content-Type: application/pdf, and I was able to reproduce the Unknown response type error.


It should either display the PDF content in the browser or it should provide link to download the file.

Swagger-UI does not support inline viewing of PDFs, nor do I expect it to do so in the future. PDF rendering is not easy to do consistently in a web application, and even then it requires heavy libraries.

Swagger-UI does allow for file downloading, as you know - however you'd need to either send Content-Type: application/octet-stream or Content-Disposition: attachment; to indicate that your response body should be downloaded by the user agent.

Learn more about Content-Disposition here.

@balaji-tech87
Copy link
Author

Hi Team,
Thanks for your quick suggestions by reproducing the issue.
I also have tried by setting Content-Type: application/octet-stream and with Content-Disposition: attachment. The pdf link is enabled and it gets downloaded by clicking it.
And thanks for confirming, Swagger-UI doesnot support inline viewing of PDF.

Suggestions:
If possible, please update the spec that "application/octet-stream" needs to used instead of "application/pdf".
Spec Link : https://swagger.io/docs/specification/describing-responses/
Under section : Response That Returns a File

@balaji-tech87
Copy link
Author

Thanks. Closing the ticket. I will raise new ticket, if any further clarification required. Thanks for your support.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants