Uploading files using the "Fage" function - data about multipart / form-data
I'm trying to do multi-page file uploads using a simulator, but I can't find a good example of this anywhere. I want the HTTP request to look something like this:
...
Content-Type: multipart/form-data; boundary=AaB03x
--AaB03x
Content-Disposition: form-data; name="name"
Larry
--AaB03x
Content-Disposition: form-data; name="file"; filename="file1.txt"
Content-Type: text/plain
... contents of file1.txt ...
--AaB03x--
Or even...
------fGsKo01aQ1qXn2C
Content-Disposition: form-data; name="file"; filename="file.doc"
Content-Type: application/octet-stream
... binary data ...
------fGsKo01aQ1qXn2C--
Do I need to manually create the request body, including creating multipage borders? This seems a little over the top considering everything else this client can do.
source to share
No no. You just need to define some kind of proxy interface, specify the content type as: multipart / form-data, and other information such as parameters required by the remote API. Here's an example:
public interface FileUploadResource {
@RequestLine("POST /upload")
@Headers("Content-Type: multipart/form-data")
Response uploadFile(@Param("name") String name, @Param("file") File file);
}
The completed example can be found here: Uploading Files with Open Feign
source to share
If you are already using Spring Web, you can try my implementation of Encoder Encoder, which can create Multipart requests. It can send a single file, an array of files, along with one or more additional JSON payloads. Here is my test project . If you are not using Spring, you can refactor your code by changing the encodeRequest method in FeignSpringFormEncoder.
source to share