Next.js Discord

Discord Forum

How do i send a message to my api and use it there

Answered
Semipalmated Sandpiper posted this in #help-forum
Open in Discord
Semipalmated SandpiperOP
When i try, and print the message(username) in the terminal, it says undefined
Answered by B33fb0n3
you can get it by awaiting your json:
const res = await request.json()

Or using formdata, when you using formdata in the future:
  const formData = await request.formData()
  const name = formData.get('name')
  const email = formData.get('email')

This is for app router. It might change, when you use the pages router. Your route handler setup looks like you are using the pages router. For pages router I couldn't find a clear way on how to access it
View full answer

4 Replies

@Semipalmated Sandpiper When i try, and print the message(username) in the terminal, it says undefined
you can get it by awaiting your json:
const res = await request.json()

Or using formdata, when you using formdata in the future:
  const formData = await request.formData()
  const name = formData.get('name')
  const email = formData.get('email')

This is for app router. It might change, when you use the pages router. Your route handler setup looks like you are using the pages router. For pages router I couldn't find a clear way on how to access it
Answer
@Semipalmated Sandpiper solved?
Semipalmated SandpiperOP
Thank you so much, i forgot to put in the await
happy to help