#sending fetch request to GET route handler

25 messages · Page 1 of 1 (latest)

jade steeple
#

I feel like i'm probably sending the fetch request incorrectly but I can't seem to get the route handler to be called.

app/api/getThing/route.ts

export 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.tsx

export 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.

dusty ravineBOT
#

🔎 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)
brittle sorrel
#

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.

jade steeple
#

I can hit it with postman and browser directly

#

maybe it's the server-side thing

#

why wouldn't that work?

brittle sorrel
#

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.

jade steeple
#

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.

brittle sorrel
#

With back and forth you mean the browser implementation of back/forward or just usual navigation that shows the data is statically cached?

jade steeple
#

browser

brittle sorrel
#

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.

jade steeple
#

yeahhh

#

anyways thanks for the help

jade steeple
thorny whale
jade steeple
#

that's so weird lol did the same exact thing

#

maybe im just bad

thorny whale
jade steeple
#

yeah just went through it

#

i already knew it was kinda shady when doing it