On this page
- Key Takeaways
- What happens before the API request?
- Diagnose the CORS preflight redirect error
- 1. Find the actual failing request in the browser
- 2. Inspect the initial preflight response without following redirects
- 3. Inspect the destination as a separate request
- 4. Retest the canonical URL in the browser
- Why curl can succeed while the browser fails
- Fix the endpoint or gateway
- Common Mistakes
- Best Practices
- Conclusion
A CORS preflight redirect error can appear before your API receives the request you intended to send. The browser first sends an OPTIONS request to ask whether the cross-origin request is allowed. If that request hits a redirect, browser behavior and the redirect chain’s CORS responses can affect whether the browser proceeds.
This guide shows how to separate the preflight from the application request, inspect each response, and find an endpoint that answers the preflight directly.
Key Takeaways
- A preflight is a separate
OPTIONSrequest; a failed preflight can prevent the subsequentPOST,PUT, or other application request from being sent. - Inspect the preflight’s status,
Location, and CORS headers without automatically following redirects. Then inspect the destination separately. curldoes not enforce browser CORS rules, so a successful command-line request does not prove the browser request will work.- Prefer a canonical API URL that responds directly to the expected preflight. Fix redirects and CORS configuration at the API or gateway when necessary.
- Redirect handling is not a universal client-side fix. Browser behavior and the details of each redirect chain matter.
The Fetch Standard and current MDN CORS documentation could not be consulted for this article. The commands below were not executed; check the current documentation and test in your supported browsers before relying on redirect-specific behavior.
What happens before the API request?
For some cross-origin requests, the browser sends an OPTIONS preflight with headers describing the intended request. For example, a JSON POST may cause a preflight that includes:
Origin: the origin of the calling page.Access-Control-Request-Method: POST: the method the browser wants to use next.Access-Control-Request-Headers: content-type: the request header the browser wants to send next.
The server must respond with CORS headers that permit the origin, method, and requested headers. Only after a successful preflight can the browser send the application request. That later request needs a suitable CORS response too.
A redirect on OPTIONS is therefore different from a redirect on the later POST. If the preflight fails, there may be no POST to inspect at all. Some cross-origin requests do not require a preflight, so reproduce the same method and headers as the failing browser request.
Advertisement
Diagnose the CORS preflight redirect error
Use an endpoint and environment you are authorized to test. The example addresses below are fictional; replace them with your frontend origin and a safe API test endpoint. Do not use a state-changing production endpoint just to reproduce the problem.
1. Find the actual failing request in the browser
Open developer tools, select the Network panel, and reproduce the failure using the application. Preserve the network log if available. Look for an OPTIONS request to the API and record:
- Its complete request URL, including scheme, host, path, and trailing slash.
- Its
Origin,Access-Control-Request-Method, andAccess-Control-Request-Headersvalues. - Its response status,
Locationheader, and anyAccess-Control-Allow-*headers. - Whether the intended application request appears after it, and whether that request has its own redirect or CORS failure.
If no OPTIONS request is visible, check whether the request actually requires a preflight, whether a cached preflight result was used, and whether the Network panel captured the relevant requests. Browser tools and caches vary; retry in a fresh browser session if the trace is unclear.
2. Inspect the initial preflight response without following redirects
In a shell, set these variables to your own values. This example models a JSON POST that requests permission to send content-type:
ORIGIN='https://app.example.com'
API_URL='https://api.example.com/v1/orders'
curl -i -X OPTIONS "$API_URL" \
-H "Origin: $ORIGIN" \
-H 'Access-Control-Request-Method: POST' \
-H 'Access-Control-Request-Headers: content-type'
The command deliberately omits -L, so a redirect response can be inspected rather than hidden by automatic redirect following. If the browser lists additional requested headers, use that same list in the command. For example, an Authorization header may require authorization to be allowed as well.
Check whether the response is a redirect, such as 301, 302, 303, 307, or 308, and read its Location. Common causes include an HTTP-to-HTTPS redirect, a missing trailing slash, a changed API hostname, or an authentication gateway redirecting to a login page. A 401 or 403 on OPTIONS can also be the problem even when there is no redirect.
3. Inspect the destination as a separate request
Resolve a relative Location against the original URL, and verify that the destination is expected and authorized before requesting it. Then repeat the preflight against that exact destination:
DESTINATION_URL='https://api.example.com/v1/orders/'
curl -i -X OPTIONS "$DESTINATION_URL" \
-H "Origin: $ORIGIN" \
-H 'Access-Control-Request-Method: POST' \
-H 'Access-Control-Request-Headers: content-type'
Repeat this process for additional redirects. Do not forward real credentials to an unverified destination. A manual request to the destination tells you how that endpoint responds; it does not prove that every browser will successfully follow the original preflight redirect.
For a direct successful response, check that:
Access-Control-Allow-Originpermits the requesting page’s origin.Access-Control-Allow-Methodspermits the intended method, herePOST.Access-Control-Allow-Headerspermits all requested headers, herecontent-type.- The response is successful rather than a login page, error, or another redirect. A server commonly uses
200or204for a successful preflight.
If the application uses credentials such as cookies, the server also needs an appropriate Access-Control-Allow-Credentials response, and a wildcard allowed origin is not sufficient for a credentialed browser response. Check the application request’s CORS headers as well; a successful preflight alone does not make its response readable to JavaScript.
Advertisement
4. Retest the canonical URL in the browser
If the destination is the intended stable API endpoint, configure the application to call that URL directly and reproduce the request. Confirm in the Network panel that the browser sends OPTIONS to the direct URL, receives the required permissions, and then sends the application request.
If the preflight succeeds but the application request fails, inspect that request separately. It may redirect, return an error, or lack the CORS headers needed for the browser to expose its response. A direct preflight does not guarantee that a later redirect or response will work across origins.
Why curl can succeed while the browser fails
A command-line client can send a request and display a response without enforcing the browser’s cross-origin access checks. A plain curl request may not send an Origin header or an OPTIONS preflight at all. Even the diagnostic OPTIONS command above only shows the server response; it does not reproduce the browser’s complete CORS and redirect processing.
Browsers can also differ in redirect handling, and behavior depends on which request redirects and on the responses in the chain. Do not infer that all preflight redirects fail, or that adding redirect: "follow" to a client request will repair a failed preflight. Check the actual browser trace and supported browser versions.
Fix the endpoint or gateway
Start with the narrowest change that makes the expected preflight reach the correct handler directly:
- Use the canonical URL. Update the frontend configuration if it points to an old host, an HTTP URL, or the wrong path form. Confirm that this is a supported endpoint, not just a temporary redirect destination.
- Handle
OPTIONSbefore authentication redirects. A preflight asks permission for a later request; it normally does not carry the application’s credentials. Configure the gateway or API to answer the expected preflight rather than redirecting it to a login page. Continue to authenticate and authorize the actual application request. - Configure CORS for the actual request. Allow only the origins, methods, and request headers the application needs. Return the appropriate CORS headers on relevant responses, including errors that the frontend needs to read.
- Check every infrastructure layer. A CDN, reverse proxy, load balancer, framework, or API gateway can generate the redirect before the request reaches application code. If the allowed origin is selected dynamically, ensure shared caches vary appropriately by
Origin; preflight responses that vary by requested method or headers also need appropriate cache handling.
Do not blindly reflect arbitrary origins or requested headers. A direct URL is useful for troubleshooting, but it does not replace authentication, authorization, or a deliberate CORS policy.
Common Mistakes
- Inspecting only the
POST. The browser may never have sent it becauseOPTIONSfailed first. - Using
curl -Las the only diagnostic. It can obscure the redirect response that needs investigation. - Testing a different request. A different origin, method, or header list may produce a different preflight result.
- Fixing only the preflight. The application response and any redirects it encounters still need to work under browser CORS rules.
- Allowing every origin as a quick fix. This can expose responses more broadly than intended and does not solve every credentialed-request case.
- Treating CORS as authentication. CORS controls browser access to cross-origin responses; it is not a substitute for server-side access control.
Best Practices
Keep API base URLs consistent across environments, test both OPTIONS and the actual request through the same public gateway, and include supported browsers in release checks. When changing redirect or CORS settings, inspect the full request chain and test error responses as well as successful responses.
Conclusion
Find out whether the redirect occurs on the preflight or on the later application request. Inspect the initial response and its destination separately, then prefer an API URL that directly answers the expected preflight. Verify the complete flow in the browser rather than treating a successful command-line response as proof that CORS is configured correctly.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.