Reputation: 11
I made one API in spring boot using Kotlin. In which one should pass a file in the form-data.
@PostMapping("/refunds")
fun uploadFileToS3(@RequestPart("file") file
: MultipartFile)
: ResponseEntity<String> {
if (!isCsvOrTsvOrExcel(file)) {
throw Exception("Only CSV, TSV and xlsx files are allowed")
}
val key = "refunds/${file.originalFilename}" // TODO("add some randomness
// in file name")
val fileUriAndRows =
uploadFileService.uploadFileToS3(file, bucketName, key)
val submerchantIds =
userSubmerchantMapService.findByUserId(getCurrentUserLogin())
.get()
.map{it.submerchantId.toInt()}
uploadFileService.saveData(file,
FileUploadDto(userId = getCurrentUserLogin().toInt(),
entityType = EntityType.REFUND.type,
status = DownloadStatus.PENDING.status,
referenceId = null, // TODO(vijay to input)
submerchantIds = submerchantIds,
requestedAt = LocalDateTime.now(),
totalRecords = fileUriAndRows.second,
inputFileName = file.originalFilename,
s3InputFileUrl = fileUriAndRows.first.toString()))
return ResponseEntity(
"File uploaded successfully: ${fileUriAndRows.first}",
HttpStatus.OK)
}
I have provided the api code above. I am calling the api using postman here is the ss of the api call that I am making.
content type header is automatically getting set by postman - value -> multipart/form-data; boundary=<calculated when request is sent> You can see in the ss I am sending the correct key "file".
I am getting this error -> 2025-02-26T12:50:15.552+05:30 WARN 553330 --- [command-center] [nio-8888-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required part 'file' is not present.]
I want to send the file in the api call. But above mentioned error is being throw and control never comes inside the controller function (verified this by adding debugger) that I provided above. I dont know why this error is being thrown. I am passing the correct key. What am I missing?
I want this api to work. Atleast control should come inside the controller function.
Upvotes: 1
Views: 21