sending fetch request to GET route handler
Unanswered
Spectacled Caiman posted this in #help-forum
Spectacled CaimanOP
I feel like i'm probably sending the fetch request incorrectly but I can't seem to get the route handler to be called.
it returns null data and I don't see any console logs.
app/api/getThing/route.tsexport async function GET(req: Request) {
console.log("hitting endpoint", req);
await fetch("https://jsonplaceholder.typicode.com/todos/1");
return new Response(JSON.stringify({ message: "Hello World!" }));
}app/test/page.tsxexport default async function Page() {
const res = await fetch("http://localhost:3000/api/getThing", {
method: "GET",
});
return <pre>{JSON.stringify(await res.json(), null, 2)}</pre>;
}it returns null data and I don't see any console logs.
24 Replies
Giant panda
Can you hit the endpoint when using tools like Postman, curl or your browser directly?
Also, don't call your own route handlers from server-side code, that won't work most of the time anyways.
Spectacled CaimanOP
I can hit it with postman and browser directly
maybe it's the server-side thing
why wouldn't that work?
Giant panda
I can't say for sure what's causing this particular issue, but it's discouraged anyways so it kinda of is a "non-issue" in the end anyways.
Spectacled CaimanOP

the whole point im doing this is because of this other problem I'm facing where I have this one page that calls drizzle
db.select... and if you navigate back/forth from that page and go back, the select doesn't retrigger. I tried all of the revalidation options in https://nextjs.org/docs/app/building-your-application/data-fetching/revalidating but none worked, so i thought maybe if i put the db select in a route handler and then send a fetch request i can use the no caching option there.Giant panda
With back and forth you mean the browser implementation of back/forward or just usual navigation that shows the data is statically cached?
Spectacled CaimanOP
browser
Giant panda
Yeah I never looked into that behavior specifically so I can't help you with that. However your attempted solution is not going to work regardless, that's what I can guarantee you.
Spectacled CaimanOP
yeahhh
anyways thanks for the help
but as near said, do not fetch your own route handlers inside server components https://nextjs-discord-common-questions.joulev.dev/fetching-own-api-endpoint-in-react-server-components
@joulev <:SW_Shrug:1087528658064920798>
Spectacled CaimanOP
huh how did that work lol
Spectacled CaimanOP
that's so weird lol did the same exact thing
maybe im just bad

@joulev you guess
well i would like to stress again that this code is not good code, see link above
Spectacled CaimanOP
yeah just went through it
i already knew it was kinda shady when doing it
but was getting desperate 

