Home > Software design >  call api to upload file with metadata using feign client
call api to upload file with metadata using feign client

Time:09-26

I created a simple api to upload a file with its metadata using spring boot, the api works fine without any problems, here is the controller :

@PostMapping(value="/v1/docs", consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
public String uploadFile(@ModelAttribute MetaData metadata, @RequestParam("file") MultipartFile file){
   // upload file logic ...
   return "id";
}

you can see how I call this api using postman : enter image description here

I deployed this api and now I want to call it in another service (developed also by spring boot), I should use feing client to do this call, so I created a simple feign client :

@FeignClient(name = "docsClient", url="<host_url>")
public interface DocsClient{
    @PostMapping(value="/v1/docs", consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
    String uploadFile(@RequestParam("file") MultipartFile file, @ModelAttribute MetaData metadata);
}

the proleme is when I call DocsClient.uploadFile, I got an error (415, unsupported mediaType) from the deployed service, when I log the request I found that the depoloyed service get the request like this :

POST /v1/docs?file=<value_of_file>

normally It should not include file or metadata at the url, but it should include it as --form instead:

--form "key=value"

How can I solve this issue?

CodePudding user response:

The order of parameters matter, when creating the client you should respect the order :

@FeignClient(name = "docsClient", url="<host_url>")
public interface DocsClient{
    @PostMapping(value="/v1/docs", consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
    String uploadFile( @ModelAttribute MetaData metadata, @RequestParam("file") MultipartFile file);
}
  • Related