Skip to content

Send Authorization Header when loading the json is not working. #2081

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

Closed
hiteshpatwari opened this issue Apr 7, 2016 · 7 comments
Closed
Assignees

Comments

@hiteshpatwari
Copy link

So, I'm trying to make my json file visible only after authentication. I've the necessary authorization requests setup but, I don't see Bearer token being attached to the request when I first load the JSON file.

  • swagger-ui version : 2.0
  • a swagger file reproducing the issue
window.swaggerUi = new SwaggerUi({
      url: url,
      headers: {"Authorization": "Bearer XYZYZYZYZZYZYZYZY="},
      supportHeaderParams: true,
      dom_id: "swagger-ui-container",
      supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
      onComplete: function(swaggerApi, swaggerUi){
        if(window.SwaggerTranslator) {
          window.SwaggerTranslator.translate();
        }

        $('pre code').each(function(i, e) {
          hljs.highlightBlock(e)
        });

        addApiKeyAuthorization();

      },
      onFailure: function(data) {
        log("Unable to Load SwaggerUI");
      },
      docExpansion: "none",
      jsonEditor: false,
      apisSorter: "alpha",
      defaultModelRendering: 'schema',
      showRequestHeaders: true
    });

    function addApiKeyAuthorization(){
      var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("api_key", "Bearer XYZYZYZYZZYZYZYZY=", "header");
      window.swaggerUi.api.clientAuthorizations.add("Authorization", apiKeyAuth);
    }

    window.swaggerUi.load();

I never see Authorization header being attached to the request.
Can somebody please help me with this.

This is the request Header I see on Chrome console :

accept:application/json;charset=utf-8,*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie: lolcookie-lolcookie
Host: 127.0.0.1
Referer:127.0.0.1/apidocs.json
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
@hiteshpatwari hiteshpatwari changed the title Send Authorization Header when loading the json. Send Authorization Header when loading the json is not working. Apr 8, 2016
@hiteshpatwari
Copy link
Author

@ponelat @webron ... please help me with this.

@nooruddin
Copy link

Did you get this working by any chance?

@krutkowski86
Copy link

@hiteshpatwari
Try to execute addApiKeyAuthorization() inside onFailure function.

@bodnia
Copy link
Contributor

bodnia commented Mar 28, 2017

There used to be an option to add api key (as query parameter) to all fetches in swagger-ui including loading json spec. Current implementation does not have such feature to add anything to fetch of json spec.
@webron could you please clarify if such feature (to add headers or query parameters to spec fetch) is planned to be implemented?

@webron
Copy link
Contributor

webron commented Mar 28, 2017

This is related to #2793 (comment). Might be worth checking with @shockey.

@shockey
Copy link
Contributor

shockey commented Apr 4, 2017

I'm a fan of @ponelat's final suggestion in the linked comment, if we're going to implement this.

@webron
Copy link
Contributor

webron commented Jun 9, 2017

Please follow #2793.

@webron webron closed this as completed Jun 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants