Nest.js 를 사용하면서 api문서화 도구인 "Swagger"를 사용하는데
여기서 스웨거란?
Swagger 는 REST API를 설계, 빌드, 문서화 및 사용하는 데 도움이되는 OpenAPI 사양을 중심으로 구축 된 오픈 소스 도구 세트입니다. - About Swagger Specification
이 스웨거를 사용하다가
"TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body."
라는 오류가 발생해서 포스팅을 해보려고 한다.
@Get 방식으로 유저 정보에 대해 불러오려고 했는데
스웨거 관련 코드를 보면 이렇다.
@ApiBody({
type: ListUserDto,
description: '유저 조회'
})
@Body() listUserDto: ListUserDto
하지만 @ApiBody / @Body 랑은 @GET 메소드랑은 동시에 사용이 불가능 하다고 한다.
해결방안은 @Get 메소드를 사용하지말고 @Post메소드랑 사용하거나,
@ApiBody / @Body 를 사용하지 않으면 해결이 될 간단한 문제이다.
구글링에서 얻은 몇가지 코멘트를 추가하겠다.
- I also got the same error on the Swagger UI. My problem was I have mistakenly marked the Api Method as GET and send data in the request body. Once I change the annotation @GET to @POST issue got resolved.
Because you used GET http method with body. If you want to have Json body, etc you need to use POST http method, For example in your controller class, top of your method:
- swagger UI는 Get 매개 변수를 FromBody로 해석하므로 curl -d플래그를 사용합니다. [FromQuery] 데코레이터를 추가하고 문제가 해결되었습니다.
- Swagger UI doesn't support GET requests with parameters
'Web > NestJS' 카테고리의 다른 글
[Nest.JS] IoC , DI란? 그리고 NestJS의 꽃 Provider (0) | 2023.01.31 |
---|---|
[Nest.JS] ParseIntPipe "Validation failed numeric string is expected" Error (0) | 2023.01.19 |
[Nest.JS] class-transform의 plainToInstance를 활용한 Object Mapping (0) | 2023.01.19 |
[Nest.JS] ERROR [ExceptionsHandler] No metadata for "Entity" was found. (0) | 2022.11.26 |
[Nest.JS] Node.js 실행중인 서버 확인 및 종료하기 - address already in use (0) | 2022.11.26 |