How to Fix: NestJS Swagger UI loads on real Android device but "Try it out" fails with CORS / Failed to fetch
NestJS Swagger UI loads on real Android device but fails with CORS / Failed to fetch due to backend IP address not being accessible from the device's network.
📋 Table of Contents
The issue you're experiencing is due to the Android device's CORS policy blocking the requests from the Swagger UI. When you click on a 'Try it out' button, it makes an HTTP request to your backend server, but since the request is coming from a different domain (the Swagger UI), the browser doesn't allow it by default.
🚀 How to Resolve This Issue
Method 1: Enable CORS on Your Backend Server
- Step 1: In your NestJS application, add the `@nestjs/common` module and import it in your main application module.
Method 2: Configure CORS on Your Backend Server
- Step 1: Create a new file named `cors.config.ts` in your project's root directory.
🎯 Final Words
By enabling CORS on your backend server, you should be able to resolve the 'Try it out' issue and test your endpoints successfully.
❓ Frequently Asked Questions
🛠️ Related Fixes
How to Fix: Pc crashes shortly after launching game (rainbow
Pc crashes shortly after launching game, possible cause: outdated grap
How to Fix: Installing an APK on a locked down phone
Installing an APK on a locked down phone: Try using a rooted device, e
How to Fix: FPS drops
FPS drops in games can be caused by high system resource usage, outdat