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

AdonisJS 5中POST请求测试咨询:原send参数方式失效

AdonisJS 5 测试POST请求的正确方式

在AdonisJS 5中,测试客户端的API已更新,不再沿用AdonisJS 4里的.send()方法传递请求体参数,可根据接口接收的数据类型选择以下方式:

1. 发送JSON格式请求体

若接口接收JSON数据,使用.json()方法传递参数:

client.post('posts').json({ title: 'Adonis 101', body: 'Post content' })

2. 发送表单格式请求体

若接口接收application/x-www-form-urlencoded类型的表单数据,使用.form()方法:

client.post('posts').form({ title: 'Adonis 101', body: 'Post content' })

完整测试示例

在单元测试文件中可按如下方式编写:

import { test } from '@japa/runner'

test('创建新文章', async ({ client }) => {
  const response = await client.post('posts').json({
    title: 'Adonis 101',
    body: 'Post content'
  })

  response.assertStatus(201)
  response.assertBodyContains({ title: 'Adonis 101' })
})

内容的提问来源于stack exchange,提问作者Andrés Felipe Echavarría

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 15:35:43