Swagger-ui: Add custom headers on "try it out" in Swagger v3.0

Created on 16 May 2017  路  11Comments  路  Source: swagger-api/swagger-ui

I am using Swagger UI 3.0 which comes with SwaggerUIBundle. My code is the same as example one in dist folder.

window.onload = function() {
  // Build a system
  const ui = SwaggerUIBundle({
    url: "my_spec_address",
    dom_id: '#swagger-ui',
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  })

  window.ui = ui
}

I searched all day to find solution to put an auth-token header when clicking try it out. But there is NONE.
There are no full documents for this project. I don't know how I can solve this problem.
Can someone help me please?

support 3.x

Most helpful comment

I've also been searching for a way to dynamically add the header Authorization: Basic xxxxx=. I'm giving the user a dropdown of keys to use to authenticate, so it would be nice to add the header for them on change. I tried .authActions.authorizePassword({passwordType:'basic', username:'user',password:'password'}), but got an error on auth.scopes.join since scopes isn't defined.

Nothing else stuck out to me in the object returned from calling SwaggerUIBundle. @webron are you thinking this functionality will be added in 3.0.11?

All 11 comments

Depending on the type of auth-token you want to add, the recommended approach would be to use the security property in your spec.

We are working on a way to customize the requests, which I hope to be released this upcoming weekend (but available in master before then).

I've also been searching for a way to dynamically add the header Authorization: Basic xxxxx=. I'm giving the user a dropdown of keys to use to authenticate, so it would be nice to add the header for them on change. I tried .authActions.authorizePassword({passwordType:'basic', username:'user',password:'password'}), but got an error on auth.scopes.join since scopes isn't defined.

Nothing else stuck out to me in the object returned from calling SwaggerUIBundle. @webron are you thinking this functionality will be added in 3.0.11?

@mccool that's our goal, no guarantees ;)

@webron Sure, I understand. The goal is to add the header Authorization: Basic base64 string here to the request sent out using the Try it out button without using the Authorization modal and without using the default menu that opens in chrome
screen shot 2017-05-16 at 11 51 14 am
. I'm guessing this may already be there I just can't find it.

In v2.x I was using

api.clientAuthorizations.add('basicAuth', new SwaggerClient.ApiKeyAuthorization("Authorization", basic, "header"))

where basic was the value I used for the Authorization header. It's the usual Basic user:pass
encoded with btoa.

I manage to typo, that's to what's(fixed now). Yes, I'm gifted. Sorry about for the confusion :)

I just noticed the edit! No worries!

Closing due to inactivity - feel free to comment if there are any further thoughts or concerns, and we'll be happy to reopen this issue.

I am using Swagger UI 3.0 which comes with SwaggerUIBundle. My code is the same as example one in dist folder.

window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "my_spec_address",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})

window.ui = ui
}

I want to pass some custom request headers . Tried adding an XMLHttpInterceptor Interceptor. Even that did not help. what is the easy way to pass custom headers as part of every request ?

@shockey To tack onto this issue, I think it would be useful to expose configurable options for fetch calls for both downloading the OpenAPI spec and by the Try It / Execution package.

Use cases would be:

  • Setting an HTTP header for all requests (i.e. CSRF / XSS tokens)
  • Modify the credentials option (somewhat related to: https://github.com/swagger-api/swagger-ui/pull/2822)

This is a somewhat of a blocker to upgrade to v3:
https://github.com/marcgibbons/django-rest-swagger/pull/637

@marcgibbons, see below:

Locking this closed support ticket! Please direct your comments to a new support ticket, or feel free weigh in on #2793.

Was this page helpful?
0 / 5 - 0 ratings