회원가입 시 이미지 선택
스토리 생성 시 이미지 선택
POST /story/create HTTP/1.1
Host: localhost:3000
Authorization: Bearer ...토큰
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="title"
1
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="content"
내 스토
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="categoryId"
2
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="place"
{"latitude": 1000.1232142412421111, "longitude":2.042142141242142, "title":"abc", "address":"fsafsa"}
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="date"
2023-11-20T14:30:00Z
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="images"; ...파일정보
Content-Type: image/webp
(data)
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="images"; ...파일정보
Content-Type: image/webp
(data)
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="images"; ...파일정보
Content-Type: image/webp
(data)
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="badgeId"
58
------WebKitFormBoundary7MA4YWxkTrZu0gW--
실제 서버에서 받을 데이터는 해당 요청이고 해당 요청을 처리하는 코드를 작성했습니다.
@Post('create')
@UseInterceptors(FilesInterceptor('images', 5))
@ApiOperation({ summary: '스토리 생성' })
@ApiResponse({
status: 200,
description: 'storyId',
type: StoryCreateResponseDto,
})
async create(@UploadedFiles() images: Array<Express.Multer.File>, @Request() req: any, @Body(new ValidationPipe({ transform: true })) createStoryRequestDto: CreateStoryRequestDto): Promise<StoryCreateResponseDto> {
const { title, content, categoryId, place, badgeId, date } = createStoryRequestDto;
const storyCreateResponseDto = await this.storyService.create(req.user.userRecordId, { title, content, categoryId, place, images, badgeId, date });
return storyCreateResponseDto;
}
해당 코드는 스토리 생성을 처리하는 Controller로, @UseInterceptors(FilesInterceptor('images', 5))
데코레이터를 통해 파일을 받아올 수 있었습니다. (FilesInterceptor에 2번째 옵션은 최대 파일의 개수를 설정하는 필드입니다.) 또한 현재 코드에는 없지만 @UploadedFiles()