Forum Discussion

severalservals's avatar
severalservals
New Contributor
5 years ago

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.

  • 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. 

  • 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.