Forum Discussion

ankkashyap's avatar
ankkashyap
New Contributor
2 months ago

requestInterceptor with Python

Hello , 

I have configured the swagger using FAST API for my project , Now I would like to intercept the request coming out of swagger UI . In swagger documentation I see a parameter requestInterceptor can be used for such task , but when ever I am starting my app with below mentioned code , Swagger UI starts giving me error "s.requestInterceptor is not a function /openapi.json" , I tried looking for the solution but found None

app = FastAPI(
    swagger_ui_parameters={"requestSnippetsEnabled": True,"persistAuthorization":True,
                           "requestInterceptor": """
           (req) => {
               // Modify the request object as needed
               req.headers['Authorization'] = 'Bearer <your_access_token>';
               return req;
           }
       """
                           
                           
                           
                           }
    
)

 

1 Reply

  • Parker's avatar
    Parker
    New Contributor

    This problem might be occurring because the way requestInterceptor is passed into FastAPI isn't correctly configuring it for Swagger UI. In FastAPI, the swagger_ui_parameters option might not support the inclusion of JavaScript functions like requestInterceptor directly.

    To fix this, you might need to modify the Swagger UI setup manually or look for alternative ways to intercept requests if FastAPI doesn't directly support this feature. Unfortunately, FastAPI's standard setup might not include support for dynamic JavaScript functions directly through the server-side configuration. You may need to explore customizing the Swagger UI bundle or using middleware for request modification.
    You may explore further.