#404 error while api post
23 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
@Risky down under
can you share what your request looks like (url and code) and what it is consuiming (ie the endpoint)
ok
import axios, { Axios } from 'axios';
import React from 'react'
const page = async () => {
const dataToSend =
{
"value1": "dhuvara",
"value2": "Solution",
"value3": "mani"
}
axios.post('http://localhost:3000/pages/api/addpagecontents',dataToSend,
{
headers: {
'Content-Type': 'application/json'
}
})
.catch((error) => {
console.log("-----------------------------------------------------------------------------------------")
console.error('Error adding data:', error);
})
return (
<div>page</div>
)
}
export default page
this my code
can you show a screenshot of your file tree (like side panel of vscode)
ahh so your using app dir
so 2 problems i see
- you shouldn't fetch your own apis inside server component https://nextjs-faq.com/fetch-api-in-rsc
- your api route needs to be in a folder and end with
route.ts
this is my error
yeah because api routes syntax has changed in between app and pages dir
in app dir, you make the file end with route.ts and the folders are the pathname
to resolve this what should i do now
edit your route file to have the addpagecontents.ts to be inside the folder named addpagecontents and have a file inside it with route.ts
and then read the docs: https://nextjs.org/docs/app/building-your-application/routing/route-handlers
but as i said here, you are using server component, so you should either make it client and use fetch or just do the code directly (no unessary requests)