Skip to content

Commit

Permalink
fix: 修正RequestParam用法
Browse files Browse the repository at this point in the history
  • Loading branch information
linyyyang committed Jan 15, 2024
1 parent 42de81b commit 5cb8ddb
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,6 @@ constructor(ctx: KoattyContext) {
也不能被其他组件引用(反模式)



### 获取参数

koatty解析和处理request参数后,在控制器中我们可以通过以下方法进行获取参数值:
Expand Down Expand Up @@ -831,6 +830,7 @@ koatty解析和处理request参数后,在控制器中我们可以通过以下
```
* RESTful API参数

通过@PathVariable装饰器获取:
```js
...
@GetMapping("/test/:id") //在方法装饰器中,申明参数
Expand All @@ -840,47 +840,57 @@ koatty解析和处理request参数后,在控制器中我们可以通过以下
...
```

通过ctx.requestParam获取:
```js
...
@GetMapping("/test/:id") //在方法装饰器中,申明参数
test(){ // 使用PathVariable获取绑定的参数
console.log(this.ctx.requestParam["id"]);...
}
...
```

* Body参数

通过@Post装饰器获取:
```js
...
@PostMapping("/get")
async get(@Post("id") id: number): Promise<any> {
@PostMapping("/post")
async post(@Post("id") id: number): Promise<any> {
console.log(id);
}
...
```

通过@RequestParam装饰器获取
通过@RequestBody装饰器获取
```js
...
@PostMapping("/get")
async get(@RequestParam("id") id: number): Promise<any> {
console.log(id);
@PostMapping("/post")
async post(@RequestBody() body: any): Promise<any> {
console.log(body.post);
}
...
```
> RequestParam装饰器既可以获取Body参数,又可以获取queryString参数。需要注意的是如果Body和queryString中有同名参数,会取Body传递的值

通过@RequestBody装饰器获取:
通过ctx.requestBody获取:
```js
...
@PostMapping("/get")
async get(@RequestBody() body: any): Promise<any> {
console.log(body.post);
@PostMapping("/post")
async post(): Promise<any> {
console.log(ctx.requestBody.post);
}
...
```
> RequestBody装饰器获取的值包括Body以及上传的文件对象

> RequestBody装饰器获取的值包括表单参数以及上传的文件对象
* 上传文件

通过@File装饰器获取:
```js
...
@PostMapping("/get")
async get(@File("filename") fileObject: any): Promise<any> {
@PostMapping("/post")
async post(@File("filename") fileObject: any): Promise<any> {
console.log(fileObject);
}
...
Expand All @@ -889,13 +899,23 @@ koatty解析和处理request参数后,在控制器中我们可以通过以下
通过@RequestBody装饰器获取:
```js
...
@PostMapping("/get")
async get(@RequestBody() body: any): Promise<any> {
@PostMapping("/post")
async post(@RequestBody() body: any): Promise<any> {
console.log(body.file);
}
...
```

通过ctx.requestBody获取:
```js
...
@PostMapping("/post")
async post(): Promise<any> {
console.log(ctx.requestBody.file);
}
...
```

* HTTP header

通过@Header装饰器获取:
Expand Down

0 comments on commit 5cb8ddb

Please sign in to comment.