content-disposition header needs to be read in order to execute the logic in https://github.com/swagger-api/swagger-ui/blob/master/src/main/javascript/view/OperationView.js#L603.
} else if (headers['Content-Disposition'] && (/attachment/).test(headers['Content-Disposition']) ||
headers['content-disposition'] && (/attachment/).test(headers['content-disposition']) ||
headers['Content-Description'] && (/File Transfer/).test(headers['Content-Description']) ||
headers['content-description'] && (/File Transfer/).test(headers['content-description'])) {
However XHRHttpRequest, that's being used to make the request, isn't fetching content-disposition header.
Line No. 29610 in https://github.com/swagger-api/swagger-ui/blob/master/dist/swagger-ui.js
request.getXHR = function () {
if (root.XMLHttpRequest
&& (!root.location || 'file:' != root.location.protocol
|| !root.ActiveXObject)) {
return new XMLHttpRequest;
} else {
try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) {}
try { return new ActiveXObject('Msxml2.XMLHTTP.6.0'); } catch(e) {}
try { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch(e) {}
try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) {}
}
return false;
};
I've checked this by making a call like so:
> var r = new XMLHttpRequest()
> r.open("GET", url, true)
> r.send()
> r.getAllResponseHeaders()
"Content-Type: image/type"
I do know that the response has the content-disposition header (via curl call).
Also when I set useJQuery to true when creating SwaggerUI, I see that it creates JQueryHttpClient at line no. 1934 in https://github.com/swagger-api/swagger-ui/blob/master/dist/swagger-ui.js
// legacy support
if ((obj && obj.useJQuery === true) || this.isInternetExplorer()) {
client = new JQueryHttpClient(opts);
}
which again, doesn't fetch the content-disposition header.
The spec is like so:
"/api/projects": {
"get": {
"tags": [
"projects"
],
"summary": "Export project data",
"description": "Export project data",
"operationId": "projects",
"produces": [
"application/octet-stream"
],
"parameters": [
{
"name": "project_ids",
"in": "query",
"description": "A list ids of the projects that we want to export",
"required": true,
"type": "array",
"items": {
"type": "integer"
}
},
{
"name": "version",
"in": "query",
"description": "a slower or faster version of the algorithm",
"required": false,
"default": "faster",
"type": "string",
"enum": [
"faster",
"slower"
]
}
],
"responses": {
"200": {
"description": "download file",
"schema": {
"type": "file"
}
}
}
}
What am I missing? Is there a version of XHRHttpClient that would actually get content-disposition header?
I am using the latest swagger-ui code, and dist folder.
Much appreciate your response. Thanks.
Rajesh
By the way I see somewhat related open tickets:
I don't see a response to my question there.
I also checked the recent tickets (https://github.com/swagger-api/swagger-ui/issues/1196#issuecomment-100376730, https://github.com/swagger-api/swagger-ui/issues/374#issuecomment-72673502) but my problem still exists, which confuses me. Were any of the solutions proposed or implemented actually merged into swagger-ui so that this issue is indeed resolved? If so, could you please send me the swagger specification that I could use to see this working? Perhaps of petstore or another demo API?
Thanks
Rajesh
Referring @fehguy to this ticket since he seems to have worked on this most recently.
This could be a CORS issue. Is the Swagger UI hosted on the same server (and port) as your application? If not, it must be using CORS to call your API. In this case, is content-disposition listed in Access-Control-Request-Headers? If not, the content-disposition header will not be available in XMLHttpRequest.
@dgreenbean i have all things correct,still its not displaying download link -
in the response.header only two headers are coming
Accept:application/octet-stream
Access-Control-Allow-Origin:*
Access-Control-Request-Headers:Content-Description,content-disposition
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Description:File Transfer
content-disposition:attachment; filename=resume.pdf
Content-Length:268288
Content-Type:application/octet-stream;charset=UTF-8
Date:Sun, 03 Jan 2016 20:42:56 GMT
Expires:0
Pragma:no-cache
Server:Apache-Coyote/1.1
Ok it worked after adding "Access-Control-Expose-Headers" header.
Thanks for following up
Most helpful comment
Ok it worked after adding "Access-Control-Expose-Headers" header.