How to Handle a Post Request in Next.js? Last Updated : 13 May, 2024 Comments Improve Suggest changes Like Article Like Report NextJS is a React framework that is used to build full-stack web applications. It is used both for front-end as well as back-end. It comes with a powerful set of features to simplify the development of React applications. In this article, we will learn about How to handle a post request in NextJS. Approach to Handling a Post Request in NextJSWe are going to use a POST request API route handler to handle the POST request in NextJS. NextJS provides different HTTP method API route handlers to handle the specific HTTP request. For example, We have created an Input field and a button. We have stored the data of the input field in a state. When a user clicks on the Send POST Request button, the sendPostRequest function will send a POST request with input field data. Steps to Setup a NextJS AppStep 1: Create a NextJS application using the following command and answer a few questions. npx create-next-app@latest app_nameStep 2: After creating your project folder, move to it using the following command. cd app_nameProject Structure: Example: The below example demonstrate the handling of post request in NextJS. JavaScript //File path: src/app/page.js 'use client'; import { useState } from "react"; export default function Home() { const [data, setData] = useState('') const sendPostRequest = async () => { let response = await fetch('http://localhost:3000/api/postfunction', { method: "POST", body: JSON.stringify(data) }) response = await response.json(); alert(response.data) } return ( <div style={{ margin: "10px" }}> <h1>Handling POST Request in Next.js</h1> <input type="text" placeholder="Enter Name" style={{ padding: "10px 5px" }} value={data} onChange={(e) => setData(e.target.value)} /> <br /><br /> <button onClick={sendPostRequest} style={{ padding: "5px 10px" }}> Send POST Request</button> </div> ); } JavaScript //File path: src/app/api/postfunction/route.js import { NextResponse } from "next/server"; export async function POST(req, res) { const data = await req.json() return NextResponse.json({ "data": data }) } Start your application using the command: npm run devOutput: Comment More infoAdvertise with us Next Article How to Handle a Post Request in Next.js? J jaimin78 Follow Improve Article Tags : Web Technologies ReactJS Next.js Similar Reads How to Send an HTTP POST Request in JS? We are going to send an API HTTP POST request in JavaScript using fetch API. The FetchAPI is a built-in method that takes in one compulsory parameter: the endpoint (API URL). While the other parameters may not be necessary when making a GET request, they are very useful for the POST HTTP request. Th 2 min read How To Send POST Request To External API In NextJS? Sending POST requests to external APIs in Next.js enables interaction with third-party services or your backend. This allows for data submission, form handling, and integration, enhancing your application's functionality.Prerequisites:NextJSJavaScriptReactJSBelow are the approaches mentioned to send 4 min read How to use CORS in Next.js to Handle Cross-origin Requests ? NextJS simplifies server-side logic with its API routes, reducing the need for separate server maintenance. CORS, usually configured on the server, can be handled within NextJS API routes directly. These routes, integrated into the application codebase, allow control over request origins. By impleme 7 min read How to Catch All Routes in Next.js ? To catch all routes in Next.js, you create a dynamic route file using the catch-all segments syntax. This allows you to handle various routes dynamically, enhancing routing flexibility and application structure.Catch all routesTo catch all routes in next js, We willCreate a file named [...gfg].js in 2 min read How to access Raw Body of a Post Request in Express.js ? Raw Body of a POST request refers to unprocessed or uninterpreted data sent in the request before Express or any middleware processes or understands it. It's like raw ingredients before the cooking begins. In this article we will see various approaches to access raw body of a post request in Express 3 min read Like