0

I am trying to intercept response messages in Swagger using this code:

var full = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');

var ui = SwaggerUIBundle({

    url: full + "/swagger/v2/swagger.json",

    responseInterceptor: function (resp) {
        console.log('#response');
        return resp;
    },

    onComplete: function () {
        console.log('#onComplete');

    }
});

The problem is the response interceptor is called only once (for the https://localhost:5001/swagger/v2/swagger.json file) and it is not called for any API messages.

Is it possible to intercept all swagger API messages?

According to this post it should be possible: https://stackoverflow.com/a/46892528/1882699, but this does not work for me for some reason.

Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
  • `responseInterceptor` works fine for me in the latest version (3.20.2) with the Petstore demo, e.g. if I clone the [repo](https://github.com/swagger-api/swagger-ui) and add `responseInterceptor` to `dist/index.html` and then use that index.html. Which version of Swagger UI do you use? (Open the browser console and evaluate the `versions` variable.) Are there any other errors in the browser console? Does Swagger UI actually receive responses from your API? It might be that the requests from UI are blocked by CORS; this would be indicated by "Failed to fetch" messages in UI. – Helen Dec 14 '18 at 08:56

1 Answers1

0

This configuration of Swagger UI works for me is in this post.

The difference is this line:

dom_id: '#swagger-ui',

When this line is used the interceptor intercepts every message. Without this line the interceptor catches only the first message.

Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63