Redirect some API requests in a Website to localhost for Dev Test / Debug
Why do you need this redirecting?
Today most developers working with API & Web Applications. When we need to debug & dev testing an API that sending from a website. Our normal approach is taking the API call from the browser network tab to the Postman and change the request endpoint to localhost and required parameters. Dev Testing and Debugging is less effective in this way. Because we have to change multiple things in our API call. Sometimes we are running API for only one endpoint. But to test an entire flow with API calls is more time-consuming.
So, what if we can redirect some selected API calls to our local endpoints?
Yes, It’s possible, in a few simple steps you can get this done. I’ve selected Google Chrome for this.
Run Chrome without web security
You cannot do this redirect with some security concerns, so we have to run chrome without Web Security for this. You can do this from the below steps.
"C:\Program Files\Google\Chrome\Application\chrome.exe" — disable-web-security — user-data-dir="C:/ChromeDevMode"
Open the shortcut, If you can see as below, You are running Chrome without security successfully.
To do the redirecting, you’ll need “Requestly” plugin. You can take that from the below link.
Requestly: Redirect Url, Modify Headers etc
Modify HTTP(s) Requests - Setup Redirects, Mock APIs Response, Modify Headers, User Scripts etc
You’ll need to disable Content security policy headers to do this. You can do that from the below steps.
Redirect API Calls
Now I have a simple REST API created to simulate Soundcloud search using Flask. I’m going to redirect SoundCloud searches to my local API which will return a static response based on keyword.
Now original API call should be redirected to Local API as below.
Now you can see your SoundCloud search redirected to the Local API.
What you can do more …
This “Requestly” can be used to do much more things, changing API call parameters, headers and etc. Check other features if you are interested. Simply you can save time taken to stuck with some postman requests using this.
We have to disable web security to do this, so you may have miss some real-world scenarios when you are doing tests using this. Also, if “Requestly” (or any other plugin which you are using to redirect) gathering your personal stuff that can be a problem. I couldn’t find any clue for that. If you know anything like that please share that with me too.
Thanks for reading, hope this will save a lot of your time. Please share your thoughts.