1

I want to request another POST endpoint which accepts no request body . when I am calling that using feign client like this :

@FeignClient(name = "Client" , url = "${URL}" , configuration = Configuration.class)
public interface FeignClient {
@PostMapping(value = "/url/abc/{id}" , consumes = {"text/html"})
@Headers("Content-Length: 0")  
void abcMethod(

            @RequestHeader Map<String, String> Headers,
            @PathVariable("id") String id
    );

}

in the Headers Map I am adding following things :

{ headers.put(ApplicationConstant.ACCEPT_HEADER_NAME, MediaType.APPLICATION_JSON.toString()); headers.put(ApplicationConstant.AUTHORIZATION, token); headers.put(ApplicationConstant.TRACE_ID , traceId); headers.put("Content-Type", "text/plain; charset=us-ascii"); //tried removing it as well. headers.put("Content-Length" , "0"); } 

But Getting Error : Getting Error :

411 Length Required

<h2>Length Required</h2>
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>

I have tried :

  1. adding content-length=0 in headers map but no luck
  2. tried adding @Body("{}") , but didnt work
  3. I the controller class I am returning ResponseEntity\<Void\>(HttpStatus.NO_CONTENT);
Jens
  • 67,715
  • 15
  • 98
  • 113

1 Answers1

0

I ran into something like this and it was a little bit annoying.

Postman works fine with the endpoint since it sets the Content-Length header to 0.

In my openfeign client, I threw in an interceptor to double check the content-length and set it accordingly, but I think it's being ignored further down the line somewhere, I don't know enough about feign to figure out exactly what's wrong.

Normally I would fix the endpoint so it's a GET instead of a POST, or put the url parameter in the body so it's a real POST (sigh) but this api is for a third party integration that I have no control over.

My hack solution is this feign interceptor.


public class ContentLengthRequestInterceptor implements RequestInterceptor {

    private static final String CONTENT_LENGTH_HEADER = "Content-Length";

    @Override
    public void apply(RequestTemplate requestTemplate) {

        var length = requestTemplate.body() != null ? requestTemplate.body().length : 0;
        if (length == 0 && requestTemplate.method().equals("POST")) {
            requestTemplate.body("hello");
            requestTemplate.header(CONTENT_LENGTH_HEADER, String.valueOf("hello".getBytes().length));
        }
    }
}

If anyone knows better please let me know