cancel
Showing results for 
Search instead for 
Did you mean: 

How to use requestInterceptor in SwaggerUIBundle call

SOLVED
Highlighted
New Contributor

How to use requestInterceptor in SwaggerUIBundle call

I'm trying to use requestInterceptor in my SwaggerUIBundle call to add a header to the outgoing request. My code is:

 

window.onload = function() {
			// Begin Swagger UI call region
            const ui = SwaggerUIBundle({
                url: '@Model.SwaggerFilename',
                dom_id: '#swagger-ui',
                requestInterceptor: (req) => { req.headers.append('myheader', 'wombats'); return req; }, 
                //request.curlOptions: [ "-H apikey: abcd"],
                presets: [
                    SwaggerUIBundle.presets.apis,
                    SwaggerUIStandalonePreset
                ],
                plugins: [
                    SwaggerUIBundle.plugins.DownloadUrl
                ],
                layout: "StandaloneLayout"
            });

 

The error I get is:

request.headers.append is not a function.

 

Could anyone explain how to do this, or point me to documentation on the request object that is passed to the requestInterceptor? 

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
New Contributor

Re: How to use requestInterceptor in SwaggerUIBundle call

I figured it out with the help for a for-in loop to get the properties of the request object. The request object that requestInterceptor gets isn't the one that's described in the Fetch API. It's just got a headers array and you can add directly to it. So:

 

req.headers['authorization-token'] = 'myheadertokenvalue';

 

works fine. 

View solution in original post

1 REPLY 1
Highlighted
New Contributor

Re: How to use requestInterceptor in SwaggerUIBundle call

I figured it out with the help for a for-in loop to get the properties of the request object. The request object that requestInterceptor gets isn't the one that's described in the Fetch API. It's just got a headers array and you can add directly to it. So:

 

req.headers['authorization-token'] = 'myheadertokenvalue';

 

works fine. 

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors