'Springdoc sends Multipart file as application/x-www-form-urlencoded and not multipart/form-data
I am using the latest version of openapi-ui 1.6.7 and I can't make a file upload endpoint work. This is my configuration of the parameter :
@PostMapping(
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
@Operation(
summary = "Create a new FileResource",
requestBody = @RequestBody(description = "File to upload")
)
public ResponseEntity<FileResourceIdPublicApiDto> create(
@Parameter(
description = "File to upload",
required = true
)
@RequestPart
MultipartFile file
When I use the "Try out" button in the generated swagger UI, I get a 415 Unsupported Media Type error.
The request headers has content-type : application/x-www-form-urlencoded
I think this is where the error comes from. The generated json from OpenApi looks like this :
{
"operationId": "create_4",
"parameters": [
...
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"required": [
"file"
],
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary",
"description": "File to upload"
}
}
}
}
},
"description": "File to upload"
},
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/FileResourceId"
}
}
},
"description": "OK"
}
},
"summary": "Create a new FileResource",
"tags": [
"File Resource"
]
}
What am I missing to send a correct request with form-data content-type ?
Solution 1:[1]
For me replacing RequestPart to RequestParam did the job! btw I was using openapi-ui 1.6.4.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tsimpragakis Vasilis |