axios rejects with status code 404 in a React app
Uncaught (in promise) Error: Request failed with status code 404Short 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.
Fix it fast
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); });2Match what you see, then jump to its fix
- 1
A 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
error.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
The collection URL works but a URL with an id returns 404The record really does not existFix
- 1
What does the error look like?
Uncaught (in promise) Error: Request failed with status code 404AxiosError: 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/restaurants2Fix
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/restaurantsConfirmed by docs.strapi.io
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" src2Fix
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/restaurantsConfirmed by axios.rest
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/restaurants2Fix
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-existConfirmed by docs.strapi.io
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:
- 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.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 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 fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix