POST 请求返回 500 内部服务器错误 Next.js

问题描述 投票:0回答:1

我尝试在 next.js 应用程序中编写一个简单的 POST 请求,并在 Postman 中测试时收到 500 内部服务器错误。我写了一个 GET 请求,效果很好。

这是我的基本代码。

import { NextRequest, NextResponse } from "next/server";

export async function POST(request: NextRequest) {
  const body = await request.json();
  return NextResponse.json(body);
};

这是我的终端中的错误: ⨯ 语法错误:JSON 后位置 31 处出现意外的非空白字符 在 JSON.parse() 在 parseJSONFromBytes (节点:内部/deps/undici/undici:5329:19) 在 successSteps 处(节点:内部/deps/undici/undici:5300:27) 在 fullReadBody (节点:内部/deps/undici/undici:1447:9) 在 process.processTicksAndRejections (节点:内部/进程/task_queues:95:5) 在异步specConsumeBody(节点:内部/ deps/undici/undici:5309:7) 在异步 POST (webpack-internal:///(rsc)/./app/api/projects/route.ts:11:18) 在异步/Users/austinwilliams/Desktop/portfolio-react/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:63251

我尝试删除异步和等待,这将删除错误,但响应是一个空对象。不知道如何排除故障,因为知道它是一个承诺并且该函数需要异步。

post error-handling next.js13 http-status-code-500 server-error
1个回答
0
投票

试试这个。

import { NextRequest, NextResponse } from "next/server";

export async function POST(request: NextRequest) {
  const body = request.body;
  return NextResponse.json(body);
}
© www.soinside.com 2019 - 2024. All rights reserved.