Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

An RTK Query request is blocked by CORS in the browser

ErrorThe browser console reports that the request was blocked by the CORS policy

Short answer

CORS is enforced by the browser, so the API must send the right headers. RTK Query adds a JSON content-type and often an authorization header, which makes the browser send an OPTIONS preflight first. Fix the server's response headers, or if you send cookies, name the exact origin, not a wildcard.

  • Applies to Redux Toolkit 2, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the browser's network tab and look at the failing request. If an OPTIONS request comes first, the preflight is the problem. Repeat it with curl to see the headers the server sends.

    curl -i -X OPTIONS http://localhost:4000/api/items \
      -H "Origin: http://localhost:5173" \
      -H "Access-Control-Request-Method: GET" \
      -H "Access-Control-Request-Headers: authorization,content-type"
  2. 2Match what you see, then jump to its fix

    1. 1The curl response has no Access-Control-Allow-Origin line.The API does not send Access-Control-Allow-Origin for your front-end originFix
    2. 2An OPTIONS request fails first, and the real request never starts.The preflight is refused because RTK Query adds headersFix
    3. 3You set credentials: 'include' in fetchBaseQuery, and the console mentions credentials or the wildcard.Cookies are sent and the server answers with a wildcard originFix

What does the error look like?

  • The browser console reports that the request was blocked by the CORS policy
  • The same request works in Postman or curl

Cause 1 of 3

The API does not send Access-Control-Allow-Origin for your front-end origin

1Check

CORS failures are decided by the browser, and the server must opt in by sending Access-Control-Allow-Origin for responses to be shared with the script. Postman is not a browser, so it ignores this.

curl -i http://localhost:4000/api/items -H "Origin: http://localhost:5173"

2Fix

Allow the origin on the API (a CORS middleware or the server's own header configuration), not in the Redux code.

Access-Control-Allow-Origin: http://localhost:5173

3Verify

The same curl now shows the header, and the browser request succeeds.

curl -i http://localhost:4000/api/items -H "Origin: http://localhost:5173"

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

The preflight is refused because RTK Query adds headers

1Check

fetchBaseQuery adds content-type: application/json for JSON bodies, and prepareHeaders is where authorization is usually set. A JSON content type or an Authorization header makes the request non-simple, so the browser sends an OPTIONS preflight that the server must answer.

grep -rn "prepareHeaders\|authorization" src/

2Fix

Make the server answer OPTIONS and list the headers you send in Access-Control-Allow-Headers.

Access-Control-Allow-Origin: http://localhost:5173
Access-Control-Allow-Headers: authorization, content-type

3Verify

The OPTIONS curl from the first step now returns the headers, and the request goes through.

curl -i -X OPTIONS http://localhost:4000/api/items -H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: GET" -H "Access-Control-Request-Headers: authorization,content-type"

Confirmed by redux.js.org

Did this fix it?

Cause 3 of 3

Cookies are sent and the server answers with a wildcard origin

1Check

fetchBaseQuery takes the standard fetch options, so credentials can be set there. For a credentialed request the server must name an explicit origin (no wildcard) and send Access-Control-Allow-Credentials: true, or the browser rejects the response.

grep -rn "credentials" src/

2Fix

Send the explicit origin and the credentials header from the server, and keep credentials: 'include' in the base query.

// client
const baseQuery = fetchBaseQuery({ baseUrl: 'http://localhost:4000/api', credentials: 'include' });

# server response headers
Access-Control-Allow-Origin: http://localhost:5173
Access-Control-Allow-Credentials: true

3Verify

The request works and the cookie is set in the browser's storage.

curl -i http://localhost:4000/api/items -H "Origin: http://localhost:5173"

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Test the API with the Origin header (curl -H) before writing client code, since browsers add it and Postman does not.
  • Keep the allowed origin list in the API's configuration per environment.
  • Set baseUrl explicitly in fetchBaseQuery so every request goes to the intended host.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

The browser blocks your API response: no Access-Control-Allow-Origin header

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.