Maaz Zindani

Loading the index…

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

Browser blocks a JSON request: Content-Type is not allowed by Access-Control-Allow-Headers

ErrorRequest header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

Short answer

Content-Type: application/json is not on the browser's no-preflight list, so the browser sends an OPTIONS request first and asks for content-type. If the preflight response does not list Content-Type in Access-Control-Allow-Headers, the real request is never sent. Allow the header in your CORS policy, and make sure the policy also answers OPTIONS.

  • Applies to ASP.NET Core 8, 9 and 10, current Chrome and Firefox, any HTTP API behind a CORS policy
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send the preflight yourself and read the response headers. Access-Control-Allow-Headers must contain every header named in Access-Control-Request-Headers.

    curl -i -X OPTIONS https://api.example.com/items \
      -H "Origin: https://app.example.com" \
      -H "Access-Control-Request-Method: POST" \
      -H "Access-Control-Request-Headers: content-type"
  2. 2Match what you see, then jump to its fix

    1. 1The OPTIONS response has no Access-Control-Allow-Headers line, or it lacks Content-TypeThe server's preflight answer does not list Content-TypeFix
    2. 2Content-Type is allowed, but Access-Control-Request-Headers also names Authorization, X-Requested-With or a custom headerWithHeaders lists some headers but not all the ones the client sendsFix
    3. 3The OPTIONS answer is 401, 404 or 405, or comes from a redirectThe CORS middleware runs in the wrong place, so OPTIONS never reaches itFix

What does the error look like?

  • Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.
  • Reason: missing token 'content-type' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel
  • Response to preflight request doesn't pass access control check

Cause 1 of 3

The server's preflight answer does not list Content-Type

1Check

A request with Content-Type: application/json, or any custom header, is preflighted. The browser compares the headers it plans to send with Access-Control-Allow-Headers in the answer. MDN gives the same error, with the header name as the missing token.

curl -s -D - -o /dev/null -X OPTIONS https://api.example.com/items -H 'Origin: https://app.example.com' -H 'Access-Control-Request-Method: POST' -H 'Access-Control-Request-Headers: content-type' | grep -i access-control

2Fix

Add the header to the policy. In ASP.NET Core use WithHeaders for a list, or AllowAnyHeader.

using Microsoft.Net.Http.Headers;

builder.Services.AddCors(options =>
{
    options.AddPolicy("Frontend", policy =>
        policy.WithOrigins("https://app.example.com")
              .WithHeaders(HeaderNames.ContentType, HeaderNames.Authorization)
              .WithMethods("GET", "POST", "PUT", "DELETE"));
});

3Verify

Repeat the preflight. The answer now has Access-Control-Allow-Headers with Content-Type.

curl -s -D - -o /dev/null -X OPTIONS https://api.example.com/items -H 'Origin: https://app.example.com' -H 'Access-Control-Request-Method: POST' -H 'Access-Control-Request-Headers: content-type' | grep -i allow-headers

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

WithHeaders lists some headers but not all the ones the client sends

1Check

ASP.NET Core only matches a policy with WithHeaders when the headers in Access-Control-Request-Headers match what the policy lists. One extra header makes the middleware decline the whole preflight, and it answers 204 without any CORS headers.

curl -s -D - -o /dev/null -X OPTIONS https://api.example.com/items -H 'Origin: https://app.example.com' -H 'Access-Control-Request-Method: POST' -H 'Access-Control-Request-Headers: content-type,authorization,x-requested-with'

2Fix

Add every header the client really sends, or use AllowAnyHeader for an API with a fixed list of trusted origins.

policy.WithOrigins("https://app.example.com")
      .AllowAnyHeader()
      .AllowAnyMethod();

3Verify

The preflight returns 204 with Access-Control-Allow-Origin and Access-Control-Allow-Headers.

curl -i -X OPTIONS https://api.example.com/items -H 'Origin: https://app.example.com' -H 'Access-Control-Request-Method: POST' -H 'Access-Control-Request-Headers: content-type,authorization'

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 3

The CORS middleware runs in the wrong place, so OPTIONS never reaches it

1Check

UseCors must sit after UseRouting and before UseAuthorization. A preflight carries no credentials, so authorization that runs before CORS rejects it. A UseHttpsRedirection redirect on an http:// request also breaks the preflight with ERR_INVALID_REDIRECT.

curl -i -X OPTIONS https://api.example.com/items -H 'Origin: https://app.example.com' -H 'Access-Control-Request-Method: POST'

2Fix

Put the middleware in the order Microsoft documents, and call the HTTPS URL directly from the client.

app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("Frontend");
app.UseAuthorization();
app.MapControllers();

3Verify

OPTIONS now returns 204 with the CORS headers, then the real POST goes through.

curl -i -X OPTIONS https://api.example.com/items -H 'Origin: https://app.example.com' -H 'Access-Control-Request-Method: POST' -H 'Access-Control-Request-Headers: content-type'

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • List the headers your front end sends in one place, and keep the CORS policy next to it.
  • Set a preflight max age so browsers cache the answer: SetPreflightMaxAge in ASP.NET Core.
  • Test the preflight with curl in CI after any change to middleware order.

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

Web apps

A POST to a Google Apps Script web app returns 405 or a CORS error

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Apps Script web apps run doPost for POST and doGet for GET, and the Apps Script guides describe no other entry points.

3 causes, each with a fix

Web apps

A Chrome extension content script cannot read another site's response

Access to fetch at 'https://api.example.com/price' from origin 'https://shop.example.net' has been blocked by CORS policy

Since Chrome 85 a content script's request is treated as a request from the web page it runs in, so the page's CORS rules apply and the extension's host permissions do not help.

3 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.