Next.js Discord

Discord Forum

API ROUTE not working !?!?!

Answered
Californian posted this in #help-forum
Open in Discord
CalifornianOP
Client side call

async function getPackageDetails(productKey: string) {
    try {
        console.log("trying to get package details") //shows in browser console
        const response = await fetch(`api/getPackage`, {
            method: 'GET',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
            },
        })


Inside app/api/getPackage/route.ts

export async function GET(req: Request) {

    console.log("getting package")//not showing in dev terminal 
     //some more logic
      return Response.json(package)
Answered by Pacific herring
change
 const response = await fetch(`api/getPackage`, {

to
const response = await fetch(`/api/getPackage`, {
View full answer

12 Replies

Pacific herring
@Californian
Pacific herring
change
 const response = await fetch(`api/getPackage`, {

to
const response = await fetch(`/api/getPackage`, {
Answer
CalifornianOP
I also realised second thing
Get can’t accept a body
Which I had
But didn’t add in the question
Because I was trying out the body and URL params method
Thanks @Pacific herring
@Californian Thanks <@967500171808231444>
Pacific herring
nw, click on my reply and then "mark solution" so others know what was causing the error
@Pacific herring nw, click on my reply and then "mark solution" so others know what was causing the error
CalifornianOP
I tried to edit title to make more informative. Is that possible or no?
Pacific herring
i'm not sure if you can edit the link but people can always search for api problems and they will find ur thread too
no worries