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

Download link in response body does not work #2132

Closed
egemenvardar opened this issue May 2, 2016 · 33 comments
Closed

Download link in response body does not work #2132

egemenvardar opened this issue May 2, 2016 · 33 comments
Assignees

Comments

@egemenvardar
Copy link

I am having a problem similar to the ones described in #374 and #1196. I have a service which returns a pdf file as attachment with Content-Disposition header. This works when I call the endpoint from the browser - file is downloaded. On Swagger UI I get a download link after calling the service with response content type application/pdf, however that link downloads an empty pdf file:

swagger_pdf_download_doesnt_work

My swagger.json is generated by Enunciate (version 2.3.0) and the relevant part looks like this:

"responses": {
  "200": {
    "schema": {
    "description": "",
    "$ref": "#/definitions/json_File"
  }, ...

I tried adding "type": "file" to the schema, but it didn't fix the problem. What could be going wrong here?

@ebetschart
Copy link

After further investigations I could narrow the problem. The downloaded pdf is not empty but some characters have changed and therefore it cannot be shown properly. All the special characters are replaced by '�'. Looks like an encoding problem:

Original pdf:

%PDF-1.4
%
5 0 obj
<<
/Type/XObject
/Subtype/Form
/FormType 1
/BBox [0 0 0 0]
/Matrix [1 0 0 1 0 0]
/Resources <<
/ProcSet 1 0 R
...

In blob:

%PDF-1.4
%����
5 0 obj
<<
/Type/XObject
/Subtype/Form
/FormType 1
/BBox [0 0 0 0]
/Matrix [1 0 0 1 0 0]
/Resources <<
/ProcSet 1 0 R
...

  • The pdf is correctly downloaded and put into a local blob object in the browser
  • When reading the pdf from the blob (the link provided in the response body) some of the characters are replaced

@dgreenbean
Copy link

This is because the xhr.responseType is not blob. This causes the contents of the response are encoded into a UTF-8 string, which cannot be converted back to the original bytes of the response. I don't have a fix, but would be very interested in finding one.

@IsmaTeddy
Copy link

I have the same problem, if I click to download the file is corrupted , but if you download the file by url from chrome or the file is perfect postman .

We may be missing some annotation of swagger ?

@dgreenbean
Copy link

The issue is the xhr.responseType is not blob, so the contents are encoded
as a string. Encoding to UTF-8 and back to bytes does not necessarily
yield the original bytes.

2016-07-21 7:34 GMT-04:00 IsmaGitHub notifications@github.com:

I have the same problem, if I click to download the file is corrupted ,
but if you download the file by url from chrome or the file is perfect
postman .

We may be missing some annotation of swagger ?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#2132 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AJxQ-LEqJ8K2eJ4JRpK474Z8UqUii-rgks5qX1kzgaJpZM4IVQ_y
.

@IsmaTeddy
Copy link

There is no solution for now?

@dgreenbean
Copy link

No. Take a look at #2282 and
swagger-api/swagger-js#812. These won't solve your
issue in themselves, but they should outline what is involved.

2016-07-21 9:14 GMT-04:00 IsmaGitHub notifications@github.com:

There is no solution for now?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#2132 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AJxQ-HVqekFBHipD0QERyRg0KtXl902Dks5qX3DAgaJpZM4IVQ_y
.

@fehguy
Copy link
Contributor

fehguy commented Jul 29, 2016

@bodnia could you please look at this and see if we can have a generalized solution for the xhr.responseType?

@fehguy
Copy link
Contributor

fehguy commented Sep 15, 2016

@dgreenbean please take a look at 4bdaeba. I believe this addresses this in a general fashion.

Leave your feedback on #2404 please

@dgreenbean
Copy link

It won't hurt, but binary -> string is a lossy operation. This means that
string -> binary will not always result in the same binary as the original
response. It might work in some cases, but there are cases where it will
fail. The only way I have found to get this to work generally is to set
the responseType on the XHR to blob. Then, when reading out the response,
the raw blob needs to be preserved without converting it to a string.

2016-09-15 4:11 GMT-04:00 Tony Tam notifications@github.com:

@dgreenbean https://github.com/dgreenbean please take a look at 4bdaeba
4bdaeba.
I believe this addresses this in a general fashion.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#2132 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AJxQ-Gz6sTPGGNh21Mz8BpBpzvyudQoeks5qqP3MgaJpZM4IVQ_y
.

@fehguy
Copy link
Contributor

fehguy commented Sep 15, 2016

Got it, let me look at that.

@fehguy
Copy link
Contributor

fehguy commented Sep 15, 2016

ok @dgreenbean fixed in the PR

@dgreenbean
Copy link

I believe that will work as long as the content contains the raw binary
content.

2016-09-15 14:30 GMT-04:00 Tony Tam notifications@github.com:

ok @dgreenbean https://github.com/dgreenbean fixed in the PR


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#2132 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AJxQ-HzXX3w4QJ65xKj3wYs95oKf-13Lks5qqY7QgaJpZM4IVQ_y
.

@ckkiran
Copy link

ckkiran commented Oct 20, 2016

Hi, Taking dist folder is enough for this fix? I see few changes in the src folder as well.

@fehguy
Copy link
Contributor

fehguy commented Oct 20, 2016

There was a release, 2.2.6 that incorporates this fix.

@ckkiran
Copy link

ckkiran commented Oct 20, 2016

Thanks. I took the 2.2.6 but problem is not gone. I have taken
only dist folder. So I am wondering if I missed out anything. Should I be
taking the contents of src folder as well? Because there is a file change
in src folder.

On Thu, Oct 20, 2016 at 8:55 PM, Tony Tam notifications@github.com wrote:

There was a release, 2.2.6
https://github.com/swagger-api/swagger-ui/releases/tag/v2.2.6 that
incorporates this fix.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#2132 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AV5LKX6Zdj-cgABHIMM8yxntH4szyxaLks5q14f6gaJpZM4IVQ_y
.

@fehguy
Copy link
Contributor

fehguy commented Oct 20, 2016

No, the dist folder is everything. You only need src if you're going to rebuild it.

If the problem is still there, please open a new ticket and share a specification and other details. then we can provide a general fix.

@milind-pawar
Copy link

milind-pawar commented Jan 16, 2017

@ckkiran did you find any solution for this. I am returning file from java code in response, but on download the file get corrupts. While when downloading from browser the URL works fine.

Using: Swagger UI 2.2.8

@fehguy
Copy link
Contributor

fehguy commented Jan 17, 2017

@milind-pawar usually this happens when the produces attribute does not specify a binary file type, or the server lies in the response, and says it's something other than what it actually is. Can you please share more about your situation?

@ckkiran
Copy link

ckkiran commented Jan 17, 2017

Yes. The problem was with encoding. In swagger-ui.js, there is a function binaryRequest. You need to add your data type there, else by default it will be encoded as unicode/text. Currently, only image and pdf data types are handled.

@milind-pawar
Copy link

@fehguy ok I will show my backend java code also the swagger UI responses i get
JAVA (server side): I am returning file and this would be a image, mp3, vedio, pdf, text etc

@GET
@Produces({MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_OCTET_STREAM})
@ApiOperation(value = "Get Case Resource by resource Id")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Success" })
@Path("attachment/{resourceId}")
public Response getCaseResource(@PathParam("resourceId") Long resourceId) {
 //........
return Response.status(Response.Status.OK).entity(file).header("Content-Disposition", "attachment ; filename=" + fileName).build();
}

swaggerui

On download this file, the file name is this as shown in the below image(which is not the actual file name) also the file gets corrupt.
on_download

Also when i hit my server URL directly on the browser I get the exact file .
please text if need any thing else?

@ckkiran
Copy link

ckkiran commented Jan 17, 2017

@milind-pawar. Try what I have suggested. It will resolve your problem - I was facing the same issue.

@milind-pawar
Copy link

milind-pawar commented Jan 17, 2017

@ckkiran thanks for your rply , I found those line of code in swagger-ui.js file, but since I am newbie I don't understand what line of code i need to attach there. :(

@ckkiran
Copy link

ckkiran commented Jan 17, 2017

return (/^image/i).test(accept) || (/^application/pdf/).test(accept) || (/^application/octet-stream/).test(accept);

@milind-pawar
Copy link

milind-pawar commented Jan 17, 2017

@ckkiran as I have updated version of swagger-ui.js i.e. 2.2.8, and this code is already in the new js, unfortunately this doesn't work for me :(.
Also i added one extra parameter - (/^multipart/form-data/).test(accept); to check if by this i could get the appropriate file.

@fehguy
Copy link
Contributor

fehguy commented Jan 17, 2017

Look at your request headers. Accept: multipart/form-data doesn't make sense.

@milind-pawar
Copy link

milind-pawar commented Jan 18, 2017 via email

@ckkiran
Copy link

ckkiran commented Jan 18, 2017

I think it should have worked if you have taken 2.2.8. Try downloading a text file and see if its corrupted. If its not corrupted then its to do with encoding and the problem will be to do with content type.

@milind-pawar
Copy link

milind-pawar commented Jan 18, 2017

Thanks @ckkiran
I tried as you said. I tried downloading a text file
Having response content type as application/octet-stream I got a download link in the response body, but got a corrupt file :(
But the actual file from server is not corrupt.

@ckkiran
Copy link

ckkiran commented Jan 18, 2017

Okay. Then its not encoding problem. There could be lot going wrong. Try different browsers if possible. Check file size after downloading. Vary the file size and check if the file size matches. Open the downloaded text file and see if its completely random or there is some text from your original file. Give a try with application/plain when downloading a text file. This is pretty much I could think of so. Hope it helps.

@carvilsi
Copy link

carvilsi commented Feb 27, 2017

I have the same problem, clicking at the download link will download corrupted file.
Tried on:
Google Chrome: Version 56.0.2924.87 (64-bit)
Safari version 10.0.3 (In this case it did not download anything: Failed to load resource: Frame load interrupted)
On a Mac OS X 10.10.5

@cogito-clarus
Copy link

I have the same issue. I'm using a controller to grab firmware from the cloud - testing with swagger = EPIC FAIL, Postman = perfect
This is a serious PITA...

@nojarp
Copy link

nojarp commented Sep 12, 2017

Any updates on this issue? I am facing the same

@shockey
Copy link
Contributor

shockey commented Sep 12, 2017

This issue won't be reopened, as Swagger-UI 2.x is no longer under development.

I'm locking this thread to prevent further bumping. If you're experiencing this with the newer 3.x series, please open a new issue 😄

@swagger-api swagger-api locked and limited conversation to collaborators Sep 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests