How to get FormData on getServerSideProps?
Unanswered
Chelsea posted this in #help-forum
ChelseaOP
Hello.
My next.js application get 'POST' request form 3rd party application, and it send req with form data as payload.
I read the sample code written by JSP. And there are
Is there a method or property in getServerSideProps's parameter(context) to get form data?
My next.js application get 'POST' request form 3rd party application, and it send req with form data as payload.
I read the sample code written by JSP. And there are
request.getParameter()
methods to get form data.Is there a method or property in getServerSideProps's parameter(context) to get form data?
17 Replies
I assume third party application hits your Next.js api route not your page, right?
I don't think
getServerSideProps
is gonna be used@Chelsea
ChelseaOP
No. Third party application sends the form data to page.
well, I would recommend send request (with form data) to the api route not a page
I wonder why it is - but you can still get the request body, right?
export async function getServerSideProps(context) {
const { req } = context;
if (req.method === 'POST') {
const { name, email } = req.body; // Accessing the form data
// Process the data (e.g., save to database)
}
return {
props: {}, // Pass props to your page component
};
}
like this
ChelseaOP
Because I can't edit the way third party sends request
import { parseBody } from 'next/dist/next-server/server/api-utils';
export const getServerSideProps = async (context) => {
const body = await parseBody(context.req, '1mb');
//...
}
ChelseaOP
Oh! Thanks
I'll try
depending on the next.js version, import might be different
ChelseaOP
No matter. It was very helpful.
did it work? @Chelsea