Next.js Discord

Discord Forum

api

Unanswered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
this is my kontakt
  const [email, setEmail] = useState("");
  const [telefon, setTelefon] = useState("");
  const [betreff, setBetreff] = useState("");
  const [nachricht, setNachricht] = useState("");

  const sendEmail = async () => {
    const res = await axios.post("/api/contact", {
      params: {
        email: email,
        telefon: telefon,
        betreff: betreff,
        nachricht: nachricht,
      },
    });
    console.log(res);
  }
and this is my pages/api/route.js
export async function POST(request, response) {
  if (request.method == "POST") {
    console.log("test");
    console.log(request.body);
  }
  return response.status(200).json({ message: "ok" });
}
and i get 404 page not found

50 Replies

it looks like you read the app directory docs for route handlers, not the pages API Routes
Sun bearOP
i red this docs but it dont work
European sprat
export default function handler(req, res) {
  if (req.method === 'POST') {
    // Process a POST request
  } else {
    // Handle any other HTTP method
  }
}
this is my pages/api/contact/route.js
export default function handler(req, res) {
  if (req.method === "POST") {
    // Process a POST request
  } else {
    // Handle any other HTTP method
  }
}
and here i am makint the request
  const [email, setEmail] = useState("");
  const [telefon, setTelefon] = useState("");
  const [betreff, setBetreff] = useState("");
  const [nachricht, setNachricht] = useState("");

  const sendEmail = async () => {
    const res = await axios.post("/api/contact", {
      params: {
        email: email,
        telefon: telefon,
        betreff: betreff,
        nachricht: nachricht,
      },
    });
    console.log(res);
  };
European sprat
you need to show the full code of it being used
Sun bearOP
its about 300 lines of code
can it be because i named my file /api/contact/route.js
should i name it contact.js
@European sprat you need to show the full code of it being used
Sun bearOP
please i need help i can pay
Chilean jack mackerel
Hi
Sun bearOP
hi
Chilean jack mackerel
are you still getting the error?
Sun bearOP
yes
please help me out
Chilean jack mackerel
okay.
so
It's your api call, right?
axios.post("/api/contact" {})
Sun bearOP
yes
Chilean jack mackerel
It means that there should be contract,js in pages/api folder.
Sun bearOP
yes
Chilean jack mackerel
but you have api/contract/route.js
Sun bearOP
no no i have contact.js
no
i have
Chilean jack mackerel
oh, really?
Sun bearOP
pages/api/contact/route.js
Chilean jack mackerel
no.
pages/api/contract.js
@Sun bear pages/api/contact/route.js
Sun bearOP
i have this#
Chilean jack mackerel
please. that's the issue.
Sun bearOP
why
Chilean jack mackerel
you need to have pages/api/contract.js
not pages/api/contract/route.js
Dutch
rename it to index.js
Sun bearOP
okay
Chilean jack mackerel
yeah, at least rename it to index.js
Sun bearOP
it
i have
now i get API resolved without sending a response for /api/contact, this may result in stalled requests.
i know the problem
Dutch
paste this
export default function handler(req, res) {
  res.status(200).json({ name: 'John Doe' })
}

into index
Sun bearOP
i thinn
Dutch
and test
Sun bearOP
yes
thanks
Dutch
np
naming conventions are different depending on whether you use app router or pages. route.ts is for app router
Chilean jack mackerel
here's my production API code
and it's front end to call the api