How to troubleshoot the CORS error in Requestly?
Tags: development, debugging, learning, developertools
Published: 2023-07-19 04:45:50

---
title: How to troubleshoot the CORS error in Requestly?
published: true
date: 2023-07-11 12:25:18 UTC
tags: development,debugging,learning,developertools
canonical_url: https://requestly.com/blog/how-to-troubleshoot-the-cors-error-in-requestly/
---

![](https://cdn-images-1.medium.com/max/1024/1*p21wGUYuy-TikvAH7nRY2Q.png)

[CORS](/blog/what-is-cors), or cross-origin resource sharing, happens when an endpoint being served from _domain-A.com_ is requested from another _domain-B.com._

The server hosting the endpoint at _domain-A.com_ first reads the _Origin_ header sent from the browser, which tells it that the request was actually made from _domain-B.com_.

The server then checks if the request is allowed from that domain and, if so, continues to serve the request by replying with its decision inside the _Access-Control-Allow-Origin_ response header.

Most production systems have very strict CORS policies, only allowing the production site to access the resources served.

Although this is ideal for users, this creates a tough situation for us developers who sometimes try to debug against a production environment.

In such situations, if your debugging setup violates the CORS policy of the site, it will soon become frustrating to keep seeing errors like these for almost every third-party network request.

![Error Message](https://cdn-images-1.medium.com/max/1024/1*rN42URdjOWy_MW9dUxy4-A.png)

### Use case:

Having tools to mitigate these errors easily can be crucial in instances where

1. You are trying to run the client locally that makes calls to production APIs.
2. You are trying to debug your live site against a locally running server. This is so powerful when done right. Please read [this article](https://requestly.com/blog/local-api-development-and-testing-productivity-for-backend-engineers/) to get the details on how to do it.

And Requestly is just the tool you need to have the perfect debugging setup.

### Modifying {action item} using the Requestly desktop app

Firstly, it is important to understand that the server defines which domains satisfy the CORS policy. This is sent as the value of the Access-Control-Allow-Origin response header.

The value of Access-Control-Allow-Origin is either the domain on which the request is allowed or \* to allow requests from all domains.

Using the Requestly Header Rule, you can easily override this header to solve your CORS issues. For this,

1. Install the Requestly browser extension and click on **New Rule** in the Top-Right.

![](https://cdn-images-1.medium.com/max/1024/1*8w1pMV3zxvMfdUWtAZ949g.png)

2. Next, Select the **Modify Headers** Rule from the list of rules. This should navigate you to the rule editor.

3. Define for which requests you want the modifications to be made.

E.g., if you are trying to bypass CORS for all requests made to [production.com/api](http://production.com/api), your matching rules could be URL contains production.com/api

Requestly provides many different ways to target requests, and you can check all of them out here — [https://requestly.com/blog/how-to-create-rules-to-modify-http-requests-in-chrome-and-safari/](http://2.%20Define%20for%20which%20requests%20you%20want%20the%20modifications%20to%20be%20made.%20%20%20%20%20%20%20%20%20%20%20E.g.,%20if%20you%20are%20trying%20to%20bypass%20CORS%20for%20all%20requests%20made%20to%20%20[production.com/api](http://production.com/api),%20your%20matching%20rules%20could%20be%20`URL`%20`contains`%20`production.com/api`%20%20%20%20%20%20%20%20%20%20%20Requestly%20provides%20many%20different%20ways%20to%20target%20requests,%20and%20you%20can%20check%20all%20of%20them%20out%20here%20%E2%80%94%20<link%20to%20docs%20for%20rule%20matching>)

4. Select the **Response Headers** option because we want to override the _Access-Control-Allow-Origin_, a response header.

5. Select the option labeled **Override Response Header.**

6. Set the Response Header to _Access-Control-Allow-Origin_ and set its value to \*

7. Give the rule a name, save it, and ensure it turns on.

### Bonus:

This should handle most of the CORS errors that you face.

But if you are having a particularly bad day that doesn’t work, you now see a new error like this one.

![](https://cdn-images-1.medium.com/max/511/1*W-aRMnt20kZ1Kkz1gS_TsQ.png)

Don’t panic; we have you covered.

This happens when the client uses the _withCredentials_ method in XHR or the _credentials_ property in Request.

You can read more about it [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials). But to fix it, all you have to do is change the value that we set in step #6 from \* to the actual value of the domain making the request

So assuming you are running the client locally, you can set the value to _localhost:3000_ ( **or whatever port your UI is running on** )

And that’s it. Hopefully, you will never have to face a CORS issue; keep this rule pinned and use it when you start debugging.