Feign Client – Error 415 / 422 When Empty POST Request

Solved problems

While working on my Spring Boot project, I encountered an interesting case that might be useful for other developers using Feign Client to communicate with external services.
I wanted to send a POST request to an external server at the endpoint:

@PostMapping("services/{id}/restart")
void restartService(@PathVariable String id)

In the documentation of the external system, I read that I should not send a body, and the service also does not return anything. Everything seemed fine, but my solution did not work for some reason. In my numerous tries and tests, I got error 415 Unsupported Media Type and 422 Unprocessable Entity.

While testing the endpoint using Postman, I discovered that when I add an empty body to the request (by sending {} or {"something":"something"}), the error does not occur. After several tries and analyzing the server documentation, I realized that even if the documentation says “empty request”, the server actually expects an empty JSON object ({}) or a specific JSON structure. I tried sending an empty String, an empty list, and my own object. Only when I added Map<String, Object> body did the next attempt succeed:

Here is the corrected version of the Feign Client:

@FeignClient(name = "externalService", url = "http://external-service-url.com")
public interface ExternalServiceClient {

@PostMapping("/services/{id}/restart")
void restartService(@PathVariable("id") String id, @RequestBody Map<String, Object> body);
}

Then in my service, I added code to send an empty JSON object:

@Service
public class ServiceRestarter {

@Autowired
private ExternalServiceClient externalServiceClient;

public void restartService(String id) {
externalServiceClient.restartService(id, Collections.emptyMap());
}
}

In the above code, Collections.emptyMap() creates an empty map, which is serialized to {} in JSON format.

This case showed me how important it is to understand what the server really expects in requests, even if the documentation is unclear or confusing. By correctly configuring the Feign Client and providing the proper JSON structure, I was able to overcome the issues. I hope this post helps other developers deal with similar problems in their projects.

Leave a Reply

Your email address will not be published. Required fields are marked *