Next.js13のapp routerのapiにPOSTしたbodyデータを取得する

postしたデータはconst res = await request.json();することによってbodyデータを取得することができる。

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

export async function POST(request: NextRequest) {
  const res = await request.json();

  try {
    await prisma.post.create({
      data: {
        title: res.title,
        content: res.constent,
      },
    });
  } catch (e) {
    console.log(e);
    return NextResponse.json({ message: "Bad Request" }, { status: 400 });
  }

  return NextResponse.json({ message: "OK" }, { status: 200 });
}
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

コメント

コメントする


The reCAPTCHA verification period has expired. Please reload the page.

目次