Swagger-ui: Support for file download

Created on 30 May 2017  路  27Comments  路  Source: swagger-api/swagger-ui

swagger-ui 3.0 does not generate a download link for files.

  • swagger-ui: 3.0
  • swagger file:
{
  "swagger" : "2.0",
  "info" : {
    "description" : "descr",
    "version" : "0.1",
    "title" : "title"
  },
  "host" : "SM05720",
  "basePath" : "/",
  "schemes" : [ "https" ],
  "paths" : {
    "/test/download" : {
      "get" : {
        "tags" : [ "download" ],
        "description" : "this is to test the download",
        "operationId" : "download XYZ",
        "produces" : [ "application/msexcel" ],
        "parameters" : [ ],
        "responses" : {
          "200" : {
            "description" : "success - download the file",
            "schema" : {
              "type" : "file"
            }
          }
        }
      }
    }
  }
}
Good first issue P2 lock-bot feature 3.x

Most helpful comment

@webron, try this:

swagger: "2.0"
info:
  title: test
  version: "1.0"

host: raw.githubusercontent.com
schemes: [https]

paths:
  /mozilla/pdf.js/master/examples/helloworld/helloworld.pdf:
    get:
      produces:
        - application/pdf
      responses:
        '200':
          description: OK
          schema:
            type: file

Swagger UI 2.2.10:
download-file

All 27 comments

@b-liberman do you have a live server we can test? It's a bit difficult to check without an actual server.

Unfortunately our servers are all behind a firewall.

@webron, try this:

swagger: "2.0"
info:
  title: test
  version: "1.0"

host: raw.githubusercontent.com
schemes: [https]

paths:
  /mozilla/pdf.js/master/examples/helloworld/helloworld.pdf:
    get:
      produces:
        - application/pdf
      responses:
        '200':
          description: OK
          schema:
            type: file

Swagger UI 2.2.10:
download-file

Thanks @hkosova!

Hi,
I tried to add type in response, but why swagger-ui (3.0.12) shows "Unknown Type: file"?

get:
      tags:
      - "glossaries"
      summary: "Get the content of a glossary"
      description: ""
      operationId: ""
      consumes:
      - "application/json"
      produces:
      - "text/plain; charset=utf-8"
      parameters:
      - in: "path"
        name: "project_name"
        required: true
        description: ""
      - in: "path"
        name: "glossary_name"
        description: ""
        required: true
        description: ""
      responses:
        200:
          description: "ok"
          schema:
            type: "file"
        400:
          description: "bad request"
        404:
          description: "not found"

@GreatYYX I assume you're talking about the generated example. Please file a separate ticket for that.

Xxx

Hello GreatYYX,

Were you going to open a ticket for the unknown type file issue? If not I can do so.

Cheers,
Paul

Hi,

I can confirm the "example" issue on 3.0.12. Download work as expected but the example says "Unknown Type: file".

Regards, Adam.

We have a pending PR to resolve the example issue - https://github.com/swagger-api/swagger-ui/pull/3266.

In the meanwhile, I defined example with a comment describing filename naming and it "hides" the error message.

Even i am facing the similar problem. The Download API returns with 200 response but i am not able download the file. I see that there is no link to download the file.
I am using latest swagger UI : 3.4.0

Even i am facing the similar problem. Please guide.
The Download API returns with 200 response but i am not able download the file. I see that there is no link to download the file.
I am using latest swagger UI : 3.4.0

With the latest I do get a download link:
http://swashbuckletest.azurewebsites.net/swagger/ui/index#/Image/Image_Put

I think this issue can be closed

The download link is only present when the response has a content-length.
swagger-ui 2 didn't require the content-length to be present in the response.

@conorlennon That issue with the content-length will be a separate issue...
and what does it mean when a response does not have a content-length?

I still believe this issue can be closed

Dynamically generated responses often don't have a content-length, particularly chunked responses.

If I remove the content-length check from the ResponseBody render method in response-body.jsx, the download link is presented for a response without a content-length, and the download link works fine.

@conorlennon sounds like an easy fix... why don't you send a Pull Request?

I've just created pull request #3895.
First time doing that, so not sure if I've done everything correctly.
Hopefully I have.

I get this response with https
Server response
Code Details
200
Response headers

x-backside-transport: OK OK connection: Keep-Alive transfer-encoding: chunked content-language: en-US content-type: application/zip date: Tue, 14 Nov 2017 11:14:12 GMT p3p: CP="NON CUR OTPi OUR NOR UNI" x-content-type-options: nosniff cache-control: no-cache, no-store, must-revalidate expires: 0 x-xss-protection: 1; mode=block content-disposition: attachment; filename=kpiexport_all_2017-11-14_2165411535502891649.zip x-powered-by: Servlet/3.0 content-security-policy: script-src 'self' 'unsafe-eval' 'unsafe-inline' strict-transport-security: max-age=43000; includeSubDomains; preload pragma: no-cache x-client-ip: 172.16.32.10 x-global-transaction-id: 72889088

And same is perfectly executed with http.

The file gets created at backend, but doe not get link in swagger to see that file
Doc1.docx

@BanitaChhabra that looks like what I would expect - you don't have the content-length (because you ave chunked transfer encoding) in the response, so the download link is not displayed.

I tried the below option through REST APIi:-
Response.ok(zippedFile)
.header("Content-Disposition", "attachment; filename=" + zippedFile.getName() )
.build();

@POST
@Consumes({ javax.ws.rs.core.MediaType.APPLICATION_JSON })
@Produces("application/zip")

Are u suggesting to set content length ?

For now, setting the content-length in the response is required to get the download link.
If pull request #3895 is accepted, then setting the content-length will no longer be required (once the change ends up in a release).

OK, #3895 is merged. It _looks_ like the content length problem was the only outstanding issue: OP never followed up, and Unknown type: file was solved in #3266.

If there's anything outstanding that I've missed, feel free to reply here and I'll reopen.

@heldersepu
I can see response headers in the swagger link you shared:
http://swashbuckletest.azurewebsites.net/swagger/ui/index#/Image/Image_Put

image

Please can you guide me how can I enable headers listing in response. I m curretly using swaggerapi/swagger-editor v3.5.0.

Second question is that which swagger-ui-themes is being used in above swagger link.

@mansoor-ul-haq look at the swagger.json for the details:
http://swashbuckletest.azurewebsites.net/swagger/ui/index#/Image/Image_Put

The theme is one of these:
https://github.com/ostranme/swagger-ui-themes
Not sure which one you saw, I change them all the time

Locking due to inactivity.
This is done to avoid resurrecting old issues and bumping long threads with new, possibly unrelated content.
If you think you're experiencing something similar to what you've found here: please open a new issue, follow the template, and reference this issue in your report.
Thanks!

Was this page helpful?
0 / 5 - 0 ratings