Maaz Zindani

Loading the index…

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

axios rejects with status code 404 in a React app

ErrorUncaught (in promise) Error: Request failed with status code 404

Short answer

A 404 from axios means the server answered and said that URL does not exist, so the request went to the wrong address. Log error.config and error.response in the catch block to see the exact URL, then fix the base URL, the path, or the resource name. With Strapi the path starts with /api and uses the plural name.

  • Applies to axios 1.x, Strapi 5, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Catch the error and print the status and the real request URL. Compare that URL, character by character, with one that works in your API client.

    axios.get(url).catch((error) => {
      if (error.response) {
        console.log(error.response.status, error.response.data);
      }
      console.log(error.config.baseURL, error.config.url);
    });
  2. 2Match what you see, then jump to its fix

    1. 1A request to /restaurants, /restaurant or /api/restaurant returns 404 while /api/restaurants worksThe path is missing the API prefix or uses the wrong resource nameFix
    2. 2error.config.baseURL and error.config.url printed together do not form the address you expectThe base URL and the request path were joined wronglyFix
    3. 3The collection URL works but a URL with an id returns 404The record really does not existFix

What does the error look like?

  • Uncaught (in promise) Error: Request failed with status code 404
  • AxiosError: Request failed with status code 404

Cause 1 of 3

The path is missing the API prefix or uses the wrong resource name

1Check

Strapi serves every endpoint under /api, and a collection uses the plural API ID (for a type with plural ID restaurants: /api/restaurants). The singular name, or no /api, gives 404.

curl -i http://localhost:1337/api/restaurants

2Fix

Use the plural ID after /api for a collection. A single document adds its documentId.

const res = await axios.get("http://localhost:1337/api/restaurants");
const one = await axios.get(`http://localhost:1337/api/restaurants/${documentId}`);

3Verify

The request resolves with status 200 and the data in res.data.

curl -i http://localhost:1337/api/restaurants

Confirmed by docs.strapi.io

Did this fix it?

Cause 2 of 3

The base URL and the request path were joined wrongly

1Check

A baseURL can be set on an instance or on axios.defaults, and a request path is then added to it. A second baseURL set elsewhere in the app changes the final address without any error until the server answers.

grep -rn "baseURL\|axios.create\|axios.defaults" src

2Fix

Create one instance with one baseURL and use only paths in calls. Print error.config on a failing call to check the final address.

export const api = axios.create({ baseURL: "http://localhost:1337/api" });

const res = await api.get("/restaurants");

3Verify

The failing call now returns 200, and error.config shows the address you meant.

curl -i http://localhost:1337/api/restaurants

Confirmed by axios.rest

Did this fix it?

Cause 3 of 3

The record really does not exist

1Check

Strapi answers 404 with a NotFoundError when the document id in the URL is unknown. A stale id kept from an old list is typical.

curl -i http://localhost:1337/api/restaurants

2Fix

Read the id from the list response and use that, and handle the 404 in the catch block instead of letting the promise go unhandled.

try {
  const res = await api.get(`/restaurants/${documentId}`);
} catch (error) {
  if (error.response?.status === 404) {
    // show a not found state
  }
}

3Verify

A request for a deleted id lands in your 404 branch and does not appear as an uncaught error.

curl -i http://localhost:1337/api/restaurants/does-not-exist

Confirmed by docs.strapi.io

Did this fix it?

How do I stop it coming back?

  • Keep one axios instance per API with a single baseURL, and keep endpoints as relative paths.
  • Always attach a catch (or use try/catch) so an error is handled and shown.
  • Log error.config.url when a request fails, not only the message.

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.