How to send Authorization header with a request in Swagger UI?

后端 未结 4 1020
孤独总比滥情好
孤独总比滥情好 2020-12-31 02:33

I have a ASP.NET Web Api 2 application. I added Swashbuckle to it (Swagger for .NET). It displays my endpoints no problem, but in order to send a request I need to attach an

4条回答
  •  Happy的楠姐
    2020-12-31 03:00

    for bearer token I've done it that way: I used swashbuckle only for generating the swagger.json file and used Swagger.Net for displaying latest SwaggerUI version (3.xx) and customizing it :

    So in my project references, I 'v added (via nuget) :

    in index.html:

    
    

    then in SwaggerUIBundle constructor:

    const ui = SwaggerUIBundle({
    ...,
    requestInterceptor: function (req) {
    req.headers = {
    'Authorization': 'Bearer ' + document.getElementById('bearer-code-
    input').value
    , 'Accept': 'application/json',
    'Content-Type': 'application/json'
    };
    return req;
    },
    ... })
    

    result display:

    I've also customized a lot of other functions (Json model binder, query string parsing, custom SwaggerGenerator to override the default behavior for ConflictingActionsResolver to be able to handle multiple route paths, but it's not in the scope of this thread)

提交回复
热议问题