Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable CORS for google chrome in android studio emulator?

I am running my localhost web app in android studio emulator using port forwarding. But from server I get CORS error for backend APIs. I solved the issue on desktop using CORS disable extension or by opening google chrome with disabled security flag. How can CORS be disabled in google chrome in mobile device or android studio emulator?

like image 225
Always_a_learner Avatar asked Nov 07 '22 06:11

Always_a_learner


1 Answers

CORS is typically something you tweak as a server side/API side kinda thing. You're developing a React app, and React is frontend only, so you're using something else as your backend. If you're using express, you can just npm install cors and then require it, and the server will accept requests or you can manually set the headers. You can do that same sorta thing with Spring, Javalin, Flask, and a host of other servers/server side frameworks.

Let's say you're more familiar with React or you don't have access to the backend, another option is to use a proxy or a middleware while you're developing, in order to get rid of those errors and trick your backend and frontend into thinking that they're not talking cross-origin. Here's a good article about a React proxy middleware that intercepts requests and changes the origin.

Edit: Just to provide a little more info, you can use browser extensions like you're doing or set security flags in your browser but that's kind of a hacky way to get around CORS protections and as you can see, when you want to use a different browser for testing your frontend or maybe you want to test on a different device (android emulator), you run into this issue again because those headers are set server side.

like image 158
TheFunk Avatar answered Nov 15 '22 06:11

TheFunk