Swagger UI - Adding multiple custom header parameters

30,530 views
Skip to first unread message

splomax

unread,
Jul 15, 2015, 3:14:49 PM7/15/15
to swagger-sw...@googlegroups.com
I'm a Swagger newbie trying to add some custom headers to every REST request I make from Swagger UI.  Like all good little developers, I pored over the Swagger UI README before ever writing a single line of code (this is a slight exaggeration).  Imagine my excitement when I reached this section:

Custom Header Parameters - (For Basic auth etc)

If you have some header parameters which you need to send with every request, use the headers as below:

swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("Authorization", "XXXX", "header"));


Note! You can pass multiple header params on a single request, just use unique names for them (key is used in the above example).


Hooray!  This is exactly what I want to do.  I won't bore the faithful Google Groupers with the details, suffice to say I need to add a handful of custom headers to all of my requests.  As such, I added the following two lines to addApiKeyAuthorization():



swaggerUi.api.clientAuthorizations.add("headerKey", new SwaggerClient.ApiKeyAuthorization("Some-Header", "Foo", "header"));
swaggerUi.api.clientAuthorizations.add("anotherHeaderKey", new SwaggerClient.ApiKeyAuthorization("Some-Other-Header", "Bar", "header"));

Look at those beautiful headers, all custom and whatnot.  So I fired up my Swagger UI and I "Tried it out!"  Let's inspect our request headers...

  1. Accept:
    application/json
  2. Accept-Encoding:
    gzip, deflate, sdch
  3. Accept-Language:
    en-US,en;q=0.8
  4. Connection:
    keep-alive
  5. Cookie:
    JSESSIONID=589E7AB62DDEBB459CE06655F655D8A7; CurrentAccountId=8041500030010824; Device=DESKTOP; SelectedLocale=en; cmTPSet=Y
  6. Host:
    localhost:8080
  7. Some-Header:
    Foo
  8. User-Agent:
    Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36

Some-Header was added, but Some-Other-Header was not!  After trying many things, it seems like it's always the first header that get's added, then no other headers get added.  

I read through the Swagger-UI source and found that you can also send multiple headers with a single call to add()  like this:

var customHeaders = {
   headerKey
:
new SwaggerClient.ApiKeyAuthorization("Some-Header", "Foo", "header")),
   anotherHeaderKey: new SwaggerClient.ApiKeyAuthorization("Some-Other-Header", "Bar", "header"))
};
swaggerUi.api.clientAuthorizations.add(customHeaders)


This resulted with the same issue - only the first header is added.

I've seen some other people online with similar issues but haven't found a solution that worked for me.  The Swagger-UI README really makes it sound like what I'm trying to do is possible, and I'm hoping that's true.  I'm really hoping I'm missing something obvious!

Thanks

Ron Ratovsky

unread,
Jul 15, 2015, 3:29:36 PM7/15/15
to swagger-sw...@googlegroups.com
Looks like you've done a lot of research before posting and kudos for it!
However... https://github.com/swagger-api/swagger-ui/issues/1444.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-----------------------------------------
http://swagger.io
https://twitter.com/SwaggerApi
-----------------------------------------

splomax

unread,
Jul 15, 2015, 4:43:29 PM7/15/15
to swagger-sw...@googlegroups.com
Holy cow!  Wow, can't believe I didn't find that issue.  I'm pointing at ponelat's changeset (here) and it works now!

Thanks!

Ron Ratovsky

unread,
Jul 15, 2015, 4:46:09 PM7/15/15
to swagger-sw...@googlegroups.com
Glad you got it working for now. Feel free to subscribe to that issue to get notifications on when it's resolved in the project itself.
Message has been deleted

Buddhika

unread,
Mar 17, 2016, 9:57:08 AM3/17/16
to Swagger
Hi, I'm new to swagger as well and i'm facing the same issue. I'm using swagger-ui 2.1.4. Since the above mentioned github issue has been closed i'm assuming it has been fixed. I want to send couple of headers with the requests, but only one header gets added every time. This is how i'm adding the headers,

window.swaggerUi.api.clientAuthorizations.add("csrf", new SwaggerClient.ApiKeyAuthorization(header, token, 'header'));
window.swaggerUi.api.clientAuthorizations.add("auth", new SwaggerClient.ApiKeyAuthorization('X-Auth', Authcode, 'header'));

Any help would be greatly appreciated :)   

Ron Ratovsky

unread,
Mar 17, 2016, 1:12:29 PM3/17/16
to swagger-sw...@googlegroups.com
We recently added a new feature that takes the need to do manual authorization configuration.
Try using the master version and just your spec in it.

Feorlen

unread,
Mar 21, 2017, 12:51:15 PM3/21/17
to Swagger
On Thursday, March 17, 2016 at 10:12:29 AM UTC-7, Ron wrote:
We recently added a new feature that takes the need to do manual authorization configuration.
Try using the master version and just your spec in it.

I'd like to configure Swagger UI to use the already logged in user's cookie to authenticate as a webapp user rather than as an external client would with username/ api key. (There are effectively two APIs with different auth requirements, in a sense one "internal" and one "external.") 

It appears it could be done by adding a custom header as some other folks already did (I know nearly nothing about JavaScript) but is this something the recent change makes possible?
Reply all
Reply to author
Forward
0 new messages