-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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
Calling an endpoint which returns file does not download content #374
Comments
+1 for this fella. definitely would help keep users in swagger ui for the entire API |
👍 A workaround that could be OK for me would be to simply prevent the zip to display as bytes in browser for it causes endless lag even for "small" files. |
+1. I have content type of application/octet-stream, and I was surprised to see all the binary displayed in the web page instead of being prompted for a download. |
(This issue appears to be the same as #600) I made an attempt to implement a 'save as file' link in the response section, which would be one way to address this. At least for browsers that support the File API, it's pretty easy from a UI perspective: when the request finishes, you create a Blob containing the content of the swagger response, use window.URL.createObjectURL to get a url to the blob, and put that in a link. (This approach still requires that the whole response be loaded into memory; I don't know how to get around that.) The problem I ran into (IIUC) is that the response contents are already coerced to Unicode by the time they're returned from swagger-js, so non-text responses will be mangled. So this might require first updating swagger-js to support binary response bodies - I think this issue is relevant: swagger-api/swagger-js#123 |
Hi, a couple thoughts.
That said, you should be able to configure swagger-ui with useJquery: true and handle the binary response from jQuery directly. |
+1 |
@fehguy thanks for your advice. I was able to get a hacky implementation working, but there are a couple obstacles to doing a clean implementation in swagger-ui:
So this feature seems to require modifying swagger-js; I see a couple options:
|
Hi @brokensandals got it. Did you look at newer versions of JQuery? I'd love to move this logic all into swagger-js. The shred http client has a pretty low-level functionality to it, and we can probably work in the code somehow. Can you share the changes you made to jQuery in a gist so I can see? |
I've mostly looked at 1.8.0 and 1.9.1 so far, though based on jquery/jquery#1525 it doesn't sound like the situation has really changed.
Here's the custom transport: brokensandals@fab1bfe#diff-4 This gist shows the diff of that with the jQuery file it's based on: https://gist.github.com/brokensandals/be6911a3f7d06f93b15d (the xhrOnUnloadAbort stuff isn't relevant). Basically it just has to set the response type on the XHR, avoid calling xhr.responseText (which would trigger errors), and copy the xhr response into the responses object. See e.g. this blog post. The rest of the commit above (which is a little noisy since I regenerated the dist files) shows how it can be used: the jQuery ajax options must set |
@brokensandals I have create a plugin that resolves this issue as you said in the comment there won't be support in jQuery any soon. The plugin here : https://github.com/acigna/jquery-ajax-native/. Good luck, hope you find it very useful. |
Thank you @tarikbenmerar ! Have you ever integrated it with swagger-ui? I'll look into this in the next few days. |
@fehguy no, I have came here because someone has referenced the issue jquery/jquery#1525. |
+1 |
Looking at the code in this pull within OperationView.js, it looks like contenType is first tested for specific types, then eventually assumed to be a generic file download (line 606-). However, when running the respective develop-2.0 branch, Chrome's Developer Tools console shows The Content-Disposition in my response is
The Swagger 2.0 Spec says to use the What's the expected Swagger JSON syntax and Header fields in order to allow file downloads? |
@hamx0r - can't comment in the behavior you experience, but spec-wise, it does say to use So response part is not a valid spec. It should be: "responses": {
"200": {
"schema": {
"type": "file"
},
"description": "CSV ZIP File"
}, ...
} FWIW, it's normally better to open new issues on rather than comment on closed ones if more than a few days have past. It's true that we get email notifications but it's easy to miss a few in the load and we can't follow comments on closed issues otherwise. You can always refer to a closed issue by using the reference number. |
When calling an endpoint that has a response with Content-Disposition attachment or a Content-Type such as application/zip, swagger will only show the bytes in the response, instead of a download prompt from the browser.
The text was updated successfully, but these errors were encountered: