You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qwik City API端点如何获取POST请求的请求体?

在Qwik City API端点中获取POST请求体的方法

你只需要通过RequestHandler的参数拿到request对象,再调用它的json()方法就能解析前端发送的JSON请求体,完整代码如下:

import type { RequestHandler } from '@builder.io/qwik-city'

export const onPost: RequestHandler = async ({ request }) => {
  // 解析JSON格式的请求体
  const body = await request.json()
  
  // 直接访问前端发送的参数
  const { username, password } = body
  
  // 这里编写你的业务逻辑,比如账号验证、数据处理等
  console.log('用户名:', username)
  console.log('密码:', password)
  
  // 示例:返回响应给前端
  return json({
    success: true,
    message: '请求体获取成功'
  })
}

补充说明

  • 因为你的前端请求设置了Content-Type: application/json,所以用request.json()是对应的解析方式;如果是表单格式的请求体,需要使用request.formData()。
  • json()是异步方法,必须配合await使用才能拿到完整的请求体数据。

内容的提问来源于stack exchange,提问作者PiWizard3852

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 22:32:06