Maaz Zindani

Loading the index…

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

A fetch POST from React comes back as 415 Unsupported Media Type

Error415 Unsupported Media Type

Short answer

A 415 means the server refused the request body because of its format, not because the data is wrong. The usual cause is the Content-Type header: it is missing, names another format than the body, has a typo in the charset, or does not match what the endpoint accepts. Read the Accept-Post header in the response, then send exactly that type.

  • Applies to React 19, Fetch API (current browsers), ASP.NET Core 9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the failed request in the browser Network tab, or repeat it with curl, and read the response headers. Some servers state the type they want in Accept-Post. Compare it with the Content-Type you sent.

    curl -i -X POST https://example.com/api/items \
      -H "Content-Type: application/json" \
      -d '{"name":"test"}'
  2. 2Match what you see, then jump to its fix

    1. 1415 on every POST; the request shows a Content-Type that is missing or is not the format of the body (for example a JSON body labelled application/x-www-form-urlencoded)The Content-Type header does not describe the body you sendFix
    2. 2415 although the header looks right at a glance, for example charset=UTF8The media type string is written in a form the server does not acceptFix
    3. 3415 only on one route; the controller action has a [Consumes] attribute naming a different typeThe endpoint accepts only another content type, so the action is not matchedFix

What does the error look like?

  • 415 Unsupported Media Type
  • Accept-Post: application/json; charset=UTF-8

Cause 1 of 3

The Content-Type header does not describe the body you send

1Check

In the Network tab, open the request and read Request Headers. The Content-Type must match the body: JSON text needs application/json. A request that carries JSON but is labelled as a form is refused with 415.

// in the browser console, on the page that sends the request
// (replace the URL with your endpoint)
fetch('/api/items', {
  method: 'POST',
  body: JSON.stringify({ name: 'test' }),
}).then(r => console.log(r.status, r.headers.get('Accept-Post')));

2Fix

Set the header yourself and send the body as a JSON string.

const response = await fetch('/api/items', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ name: 'test' }),
});
if (!response.ok) {
  throw new Error(`Response status: ${response.status}`);
}

3Verify

The same request now returns a 2xx status in the Network tab.

curl -i -X POST https://example.com/api/items -H "Content-Type: application/json" -d '{"name":"test"}'

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

The media type string is written in a form the server does not accept

1Check

Servers can be strict about the exact text. Writing UTF8 instead of UTF-8 for the charset can make the server treat the whole media type as invalid. Look for a charset parameter in the header your code or an axios or fetch wrapper sets.

grep -rn "charset" src/

2Fix

Use the standard spelling, or drop the charset parameter and send application/json alone.

headers: { 'Content-Type': 'application/json; charset=UTF-8' }

3Verify

Repeat the request and confirm the status is no longer 415.

curl -i -X POST https://example.com/api/items -H "Content-Type: application/json; charset=UTF-8" -d '{"name":"test"}'

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 3

The endpoint accepts only another content type, so the action is not matched

1Check

On an ASP.NET Core API, an action marked [Consumes(...)] accepts only the listed Content-Type, and any other request gets 415. Without the attribute, the Content-Type header decides which input formatter reads the body. Look at the action you call.

grep -rn "Consumes" Controllers/

2Fix

Send the type the action names. If the action should take JSON, change the attribute to application/json. If it should take a form, send a form body with the form content type instead of JSON.

// C#
[Consumes("application/json")]
[HttpPost]
public IActionResult Create([FromBody] Item item) => Ok(item);

3Verify

Call the route again with the type the attribute lists and check for a 2xx response.

curl -i -X POST https://localhost:5001/api/items -H "Content-Type: application/json" -d '{"name":"test"}'

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Wrap fetch in one helper that always sets Content-Type for JSON requests, so no call site forgets it.
  • Check response.ok after every fetch, because fetch does not reject on a 415 or any other HTTP error status.
  • Return Accept-Post (or document the accepted types) from your own API so clients can see what to send.
  • Test new endpoints with curl and an explicit Content-Type before writing the React code.

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

Next step

Want this handled for good?

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