question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unrecognized response type - application/pdf displayed incorrectly in response body

See original GitHub issue

Q&A

  • Method of installation: dist
  • Swagger-UI version: e.g. 3.23.0
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

Swagger/OpenAPI definition:

  /pdf/{date}/lunch.pdf:
    get:
      tags:
        - PDF
      summary: Mittagessen als PDF (Woche)
      parameters:
        - name: date
          in: path
          description: YYYY-MM-DD
          required: true
          schema:
            type: string
      responses:
        '200':
          description: OK
          headers:
            Content-Disposition:
              schema:
                type: string
                example: filename=\lunch.pdf\
          content:
            application/pdf:
              schema:
                type: string
                format: binary
              example: PDF-Datei

Describe the bug you’re encountering

When you are calling an API in the try it out-section which returns an PDF-File the response body is not able to render the file or present it in a readable format. Instead you get the following message: Unrecognized response type; displaying content as text. - even if the correct content type is passed in the response header. Additionally the binary data will be displayed in the response body.

To reproduce…

Steps to reproduce the behavior: In Swagger-UI

  1. Navigate to the specific path
  2. Click on ‘Try it out’
  3. ‘Execute’
  4. Inspect Response body

Expected behavior

The PDF should be rendered as an typical pdf file in the swagger-ui or you should provide a download link to extract the file.

Screenshots

HTTP Request-Response Swagger-UI Display

Additional context or thoughts

Unfortunatly i cannot provide a running API which returns a valid PDF-File to debugging this issue Whenever you run the command curl -X GET "http://server.de/menupdfservice/api/pdf/pdf/2019-07-26/lunch.pdf" -H "accept: application/json which the Swagger-UI is calling outside of Swagger-UI you get the binary data as well. But when you try to merge this data with curl -X GET "http://server.de/menupdfservice/api/pdf/pdf/2019-07-26/lunch.pdf" -H "accept: application/json --output lunch.pdf in a spefic file with extension you get a properly working PDF-file

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:7

github_iconTop GitHub Comments

11reactions
Pzixelcommented, Dec 8, 2020

In the end it worked when I added headers to the response:

res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=labels.pdf');
2reactions
admalleddcommented, Mar 15, 2022

I don’t have the tooling on-hand right now, but I wonder if it makes sense to extend the if-statement here to include /^application\/octet-stream/i.test(contentType) || /^application\/pdf/i.test(contentType) ...? response-body.jsx#L61

I have a situation where we need to prefer the user/browsers view the PDFs instead of directly downloading first. So having the header read content-disposition: inline; filename=SomeFile.pdf vs attachment is what we need. note that content-disposition: attachment; filename=SomeFile.pdf doesn’t complain about the “Unrecognized Response Type” because it short-circuits into the “if” as an attachment/download.

A more-correct solution would be somehow in the “else” side of the statement where it checks for json, xml, images, etc, to check for PDF and do a inline-viewer magic (pdf.js? no preference from me, just “something”)

Read more comments on GitHub >

github_iconTop Results From Across the Web

swagger-Unrecognized response type; displaying content as ...
I have an endpoint that returns files ( pdf-doc or xlsx). When i test my endpoint on swagger it rturns me 200 response...
Read more >
Media Types - Swagger
Media type is a format of a request or response body data. Web service operations can accept and return data in different formats,...
Read more >
How to open a pdf downloaded from an API with JavaScript
Hi I just have a quick question, If I were to use a Base64 string to encode the URI as the file to...
Read more >
RFC 3261: SIP: Session Initiation Protocol
Content -Type contains a description of the message body (not shown). ... For example, if a UAC receives an unrecognized response code of...
Read more >
REST API Developer Guide
Salesforce IDs in response bodies are always 18-character IDs. ... REST API in many ways, but here we show you how to use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found