Request Body is not properly encoded and hidden when using spring form encoder in Feign Client

Viewed 949

I have added the necessary dependencies for spring open feign like mentioned in https://github.com/OpenFeign/feign-form and followed the mentioned configuration for feign-client.

Whenever I sent the post request with content-type as application/x-www-form-urlencoded. The request body is not generated properly.

EmailClient.java

@FeignClient(name = "email", url = "localhost:3000",
     configuration = EmailClientConfiguration.class)

public interface EmailClient {

    @PostMapping(value = "/email/send")
    ResponseDto sendEmail(@RequestBody Map<String, String> requestBody);
        
}

This is my client configuration Class:

public class EmailClientConfiguration  {
    
    @Bean
    public RequestInterceptor requestInterceptor(Account<Account> account) {
        return template -> {
             template.header("Content-Type", "application/x-www-form-urlencoded");
        };
    }
    
    @Bean
    public OkHttpClient client() {
        return new OkHttpClient();
    }
    
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
    
    @Bean
    public Decoder feignDecoder() {
        return new JacksonDecoder();
    }
    
    @Bean
    public Encoder feignFormEncoder () {
        return new SpringFormEncoder(new JacksonEncoder());
    }
}
    
    
Map<String, String> requestBody = new HashMap<>();
requestBody.put("username", "xyz");
requestBody.put("email", "xyz@gmail.com");
requestBody.put("key", "xxx");

when I called the sendEmail method in interface, the requester headers are set correctly but the request body is sent as

{"{\n  \"key\" : \"xxx\",\n  \"email\" : \"xyz@gmail.com\",\n  \"username\" : \"xyz\"\n}"

Can someone please help on this. Why request body is sent like this. And also the request body is not hidden in the server side though content-type is application/x-www-form-urlencoded.

1 Answers

It works fine after adding consumes.

@FeignClient(name = "email", url = "localhost:3000",
     configuration = EmailClientConfiguration.class)

public interface EmailClient {

    @PostMapping(value = "/email/send", consumes = "application/x-www-form-urlencoded")
    ResponseDto sendEmail(@RequestBody Map<String, String> requestBody);
    
}
Related