I would like to send arbitrary number of images during multipart/form-data request. I've found this example in guide:
https://github.com/OpenFeign/feign-form?tab=readme-ov-file#multipartform-data
// FormData parameter
@RequestLine("POST /send_photo")
@Headers("Content-Type: multipart/form-data")
void sendPhoto (@Param("is_public") Boolean isPublic, @Param("photo") FormData photo);
Which works fine, but I can't get it to work for multiple files. I've tried changing example above to accept List<FormData>
instead of FormData
but it doesn't work for me - files are not uploaded. So for example:
// client method:
@RequestLine("POST /send_photo")
@Headers("Content-Type: multipart/form-data")
void sendPhoto (@Param("photo") FormData photo);
// using above method
var myImageStream = MyClass.class.getResourceAsStream("/test.jpg");
var myImage = FormData.builder()
.fileName("test.jpg")
.contentType("image/jpeg")
.data(myImageStream.readAllBytes())
.build();
client.sendPhoto(true, myImage);
works fine, but when I change signature to accept List:
// client method:
@RequestLine("POST /send_photo")
@Headers("Content-Type: multipart/form-data")
void sendPhoto (@Param("photo") List<FormData> photo);
// using above method
var myImageStream = MyClass.class.getResourceAsStream("/test.jpg");
var myImage = FormData.builder()
.fileName("test.jpg")
.contentType("image/jpeg")
.data(myImageStream.readAllBytes())
.build();
client.sendPhoto(true, List.of(myImage));
it doesn't work. How should I specify multiple files (arbitrary number)?
Edit: I don't control the server side but according to provided open api docs it accepts multiple files. Relevant part of open api spec:
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"required": [
"dto"
],
"type": "object",
"properties": {
"dto": {
"$ref": "#/components/schemas/MyTypeDto"
},
"files": {
"type": "array",
"items": {
"type": "string",
"format": "binary"
}
}
}
}
}
}
},
So as you can see it accepts multiple files and when I use for example RestAssured library I can upload multiple files by doing:
RequestSpecBuilder requestSpecBuilder = new RequestSpecBuilder();
requestSpecBuilder.addMultiPart("files", (new File("path/to/an/image1.jpg")));
requestSpecBuilder.addMultiPart("files", (new File("path/to/an/image2.jpg")));
requestSpecBuilder.addMultiPart("files", (new File("path/to/an/image3.jpg")));
I found the answer after going with debugger and reading a little bit of Feign source code.
I'm using feign-form module to handle multiparts and the FormEncoder class handles multipart requests using MultipartFormContentProcessor which have couple different strategies for writing output. One of them is ManyFilesWriter which is able to output multiple files in appropriate format.
So providing list of files:
@RequestLine("POST /send_photo")
@Headers("Content-Type: multipart/form-data")
void sendPhoto (@Param("photo") List<File> photo);
work just fine but it's not possible (at the moment) to pass List of FormData objects.