loo*_*ool 26 typescript reactjs next.js next.js13
我试图向我的 api 发送一个 post 请求。但是,尝试在路由处理程序中访问请求正文会导致以下错误:
代码:
export async function POST(request: Request) {
const postBody: postProps = JSON.parse(request.body) // Error here on request.body
...
}Run Code Online (Sandbox Code Playgroud)
错误: “ReadableStream”类型的参数无法分配给“string”类型的参数。
任何帮助,将不胜感激
Mic*_*ael 52
您需要调用json()Request 对象。
export async function POST(request: Request) {
const res = await request.json() // res now contains body
}
Run Code Online (Sandbox Code Playgroud)
我在使用 App Router 验证 Next.js 中 Clerk 的 webhook 时遇到问题。
以下内容对我有用。
import { Webhook } from "svix";
export async function POST(req: NextRequest) {
const svix_id = req.headers.get("svix-id") ?? '';
const svix_timestamp = req.headers.get("svix-timestamp") ?? '';
const svix_signature = req.headers.get("svix-signature") ?? '';
const body = await req.text(); // This get's the raw body as a string
const sivx = new Webhook("your_secret_key_here")
const payload = sivx.verify(body, {
"svix-id": svix_id,
"svix-timestamp": svix_timestamp,
"svix-signature": svix_signature,
});
// The payload is the json.
console.log(payload);
// The rest of your code
return NextResponse.json(null, { status: 200 })
}
Run Code Online (Sandbox Code Playgroud)
以供参考:
https://developer.mozilla.org/en-US/docs/Web/API/Response/text
| 归档时间: |
|
| 查看次数: |
24176 次 |
| 最近记录: |