spring-cloud / spring-cloud-openfeign

Support for using OpenFeign in Spring Cloud apps
Apache License 2.0
1.22k stars 786 forks source link

Response body null when Request contains body (For 401 responses) #1086

Closed PedroAmLemos closed 2 months ago

PedroAmLemos commented 2 months ago

Describe the bug Using <spring-cloud.version>2023.0.3</spring-cloud.version>

When I make a OpenFeign client that does not have a body, the 401 response contains a not null body but if I add a body parameter the returned Response.Body comes empty.

Sample

@FeignClient(name = "example", url = "http://localhost:8080")
public interface Example {
    @PostMapping(value = "/post", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    ResponseEntity<String> exec(
            @RequestHeader("X-A") String a,
            @RequestBody String jsonBody
    );

this produces

feign.FeignException$Unauthorized: [401] during [POST] to [http://localhost:8080/post] [example#exec(String,String)]: []

but after removing the jasonBody attribute, I get:

feign.FeignException$Unauthorized: [401] during [POST] to [http://localhost:8080/post] [example#exec(String)]: [RETURNED BODY]

This is the controller I used to test it:

    @PostMapping(path = "/post", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    ResponseEntity<String> post(@RequestHeader("X-A") String A,String body) {
        System.out.println("request received, header " + A + " body: " + body);
        return new ResponseEntity<>("RETURNED BODY", HttpStatus.UNAUTHORIZED);

    }
pxpy commented 2 months ago

It seems like the controller problem. The parameter of jsonBody don't have @RequestBody annatation.

PedroAmLemos commented 2 months ago

@pxpy You are right! I guess the service I'm making a request to, has the same fault on their controllers. Thanks!