An RTK Query request is blocked by CORS in the browser
The browser console reports that the request was blocked by the CORS policyShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
The curl response has no Access-Control-Allow-Origin line.The API does not send Access-Control-Allow-Origin for your front-end originFix - 2
An OPTIONS request fails first, and the real request never starts.The preflight is refused because RTK Query adds headersFix - 3
You set credentials: 'include' in fetchBaseQuery, and the console mentions credentials or the wildcard.Cookies are sent and the server answers with a wildcard originFix
- 1
What does the error look like?
The browser console reports that the request was blocked by the CORS policyThe 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:51733Verify
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
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-type3Verify
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
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: true3Verify
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
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 fixWeb apps
Axios sends no cookie to an Express API even with withCredentials set to true
The login response has a Set-Cookie header, but the browser stores nothing
withCredentials only tells the browser to attach cookies.
4 causes, each with a fixWeb apps
response.json() fails after fetch with mode: no-cors
response.type is "opaque" and response.status is 0
no-cors does not bypass CORS; it hides the response.
4 causes, each with a fixWeb apps
fetch() throws TypeError: Failed to fetch in the browser
Uncaught (in promise) TypeError: Failed to fetch
Failed to fetch means the request never produced a response the browser would hand to your code.
4 causes, each with a fixWeb apps
Socket.IO client fails to connect with xhr poll error
connect_error: xhr poll error
xhr poll error is only the reason string on the client's connect_error event: the first HTTP request to the server failed.
4 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fix