I am trying to send multipart/form-data request as Map object of different types like below.
public static Map<String, Object> RequestBody(MultipartFile multipartfile)
Map<String, Object> body = new HashMap<>();
body.put("to", "abc@gmail.com");
body.put("to", "xyz@gmail.com");
body.put("file", multipartfile);
return body;
end
Now trying to send this map to feign client.
MyClient.java
@FeignClient(name = "email", url = "${email_url}",
configuration = Configuration.class)
public interface EmailClient {
@PostMapping(value = "/email/send", consumes = MULTIPART_FORM_DATA_VALUE)
Response sendEmail(@RequestBody Map<String, ?> requestBody);
}
I am getting this encoding error while sending the request
feign.codec.EncodeException: Getting multipart file's content bytes error
at feign.form.spring.SpringSingleMultipartFileWriter.write(SpringSingleMultipartFileWriter.java:47)
at feign.form.multipart.AbstractWriter.write(AbstractWriter.java:36)
at feign.form.MultipartFormContentProcessor.process(MultipartFormContentProcessor.java:87)
at feign.form.FormEncoder.encode(FormEncoder.java:105)
at feign.form.spring.SpringFormEncoder.encode(SpringFormEncoder.java:84)
at feign.ReflectiveFeign$BuildEncodedTemplateFromArgs.resolve(ReflectiveFeign.java:372)
at feign.ReflectiveFeign$BuildTemplateByResolvingArgs.create(ReflectiveFeign.java:224)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:74)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
I am already configured feign form encoder for feign and the request is being sent successfully if no file is present in the request body.
Whether Spring feign client doesn't support this type of dynamic request body?
How can we achieve this feign? RequestPart will not help because I need to send files in request body also dynamically means file sending request will be dynamic for each request like file1, file2..
Comment From: cbezmen
https://github.com/spring-cloud/spring-cloud-openfeign/blob/5aa1b7a5298d985e9e103da3752c932b0ebc4200/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/valid/ValidFeignClientTests.java#L352 I think it is already supported. Please check your spring version
Comment From: OlgaMaciaszek
Yes, @hemaraj-gajaraj, as @cbezmen has indicated, it should work. What version of Spring Cloud are you using? If the issue persists, please provide a minimal, complete, verifiable example that reproduces the issue.