Swagger-UI: Headers not sent

4,018 views
Skip to first unread message

Jorge Rodríguez

unread,
Oct 29, 2012, 3:00:24 PM10/29/12
to swagger-sw...@googlegroups.com
Hi everyone!

I'm trying get my resource list from my localhost API server (made in Rails). In order to do this, basic authorization is needed, so I just added the header and set the 'supportHeaderParams' to true. However, this doesn't seem to work, as my server sends a 302 response, so the user is not authenticated and can't get the resource listing. When debugging in my browser, I can see the request and the authorization header is not set along with the request. I've been stuck in this for a couple of hours and I can't make it work. Do you guys have any idea of why is this not working?

Here's the code:

$(function () {
            window.swaggerUi = new SwaggerUi({
                discoveryUrl:"http://localhost:3000/ninive-918/api/v1/resources.json",             
                dom_id:"swagger-ui-container",               
                supportHeaderParams: true,
                headers: { "Authorization": "Basic UUxrNG5ocENHYTJQbTJzRnlYZE46Zm9v" },
                supportedSubmitMethods: ['get']
            });

            window.swaggerUi.load();
        });


That exact header works perfectly when doing it through cURL, with the following command:

curl -H 'Authorization: Basic UUxrNG5ocENHYTJQbTJzRnlYZE46Zm9v' -X GET http://localhost:3000/ninive-918/api/v1/resources.json

tony tam

unread,
Oct 30, 2012, 8:51:25 PM10/30/12
to swagger-sw...@googlegroups.com
Hi Jorge,
Can you please make sure your API is setting the Access-Control-Allow-Headers values correctly?  If you look at your network request to the server in chrome developer tools, you should see an error like this if not correctly set:

XMLHttpRequest cannot load http://petstore.swagger.wordnik.com/api/pet.json/3. Request header field Authorization is not allowed by Access-Control-Allow-Headers.

Jorge Rodríguez

unread,
Oct 31, 2012, 6:22:59 AM10/31/12
to swagger-sw...@googlegroups.com
Hi Tom,
Thanks for your help.

I've managed to find the reason. I was trying to call the API from another domain (directly from the filesystem). Have you considered showing some feedback when this is happening? I was clueless about what was happening.

Balaji Kathi

unread,
Apr 19, 2013, 5:35:33 AM4/19/13
to swagger-sw...@googlegroups.com

Hi,
I downloaded swagger (1.1.7) from GitHub
And I have added below mentioned changes in Index.html to pass headers parameters in the Request.
supportHeaderParams:true,
headers: { "Authorization": "XXXX", "someOtherHeader": "YYYY" },


$(function () {
window.swaggerUi = new SwaggerUi({

discoveryUrl:"http://petstore.swagger.wordnik.com/api/api-docs.json",
apiKey:"Authorization",


dom_id:"swagger-ui-container",
supportHeaderParams:true,

apiKeyName: "Authorization",

headers: { "Authorization": "XXXX", "someOtherHeader": "YYYY" },
supportedSubmitMethods: ['get', 'post', 'put'],
onComplete: function(swaggerApi, swaggerUi){
if(console) {
console.log("Loaded SwaggerUI")
console.log(swaggerApi);
console.log(swaggerUi);
}
$('pre code').each(function(i, e) {hljs.highlightBlock(e)});
},
onFailure: function(data) {
if(console) {
console.log("Unable to Load SwaggerUI");
console.log(data);
}
},
docExpansion: "none"
});

        window.swaggerUi.load();
    });

But header parameters are not sent with the request the below mentioned is my request and response headers.
Request Headers
Accept application/json, text/javascript, /; q=0.01
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection keep-alive
Host localhost:8080
Origin null
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101
Firefox/7.0.1
Response Headers
Access-Control-Allow-Head... Content-Type,Authorization
Access-Control-Allow-Meth... GET, POST, DELETE, PUT
Access-Control-Allow-Orig... *
Access-Control-Request-He... Authorization
Content-Length 187
Content-Type application/json
Date Fri, 19 Apr 2013 05:42:35 GMT
Server Apache-Coyote/1.1
Please help me in resolving the issue?

tony tam

unread,
Apr 19, 2013, 12:49:10 PM4/19/13
to swagger-sw...@googlegroups.com
Hi, can you grab the distribution from here:


We're up to version 0.1.13 and can't post to the downloads anymore.
Reply all
Reply to author
Forward
0 new messages