FeignClient return an empty response body (response.body()) whenever the response status code is 401. I have did a direct call to the server through postman and the response is returning as expected with proper body. So no problem on server side.

I have also tried ApacheHttpClient as well as OkHttpClient and still the issue exits.

Version used: JDK 11, openfeign 2.2.6

You can find below snapshots of the dependencies I am using as well as configuration:

         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-io</groupId>
                    <artifactId>commons-io</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-okhttp</artifactId>
        </dependency>
feign:
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 30000
        loggerLevel: NONE

Comment From: evgenijbogdanov

In my case I solved this problem by adding this dependencies:

implementation platform("org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}") implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' implementation group: 'io.github.openfeign', name: 'feign-okhttp', version: '11.1'

springCloudVersion = '2020.0.1'

Then, I created a config class for client, where set a bean of OkHttpClient:

` public class ClientConfiguration {

@Bean
public ErrorDecoder errorDecoder() {
    return new CustomWebClientErrorDecoder();
}

@Bean
public OkHttpClient client() {
    return new OkHttpClient();
}

} ` Finally, I set this config class as "Configuration" in @FeignClient annotation:

@FeignClient(value = "webclient" configuration = ClientConfiguration.class) public interface WebClient {

Comment From: nada-gamal

@evgenijbogdanov I tried this way and still didn't work

Comment From: OlgaMaciaszek

Hello, @nada-gamal, please provide a minimal, complete, verifiable example that reproduces the issue - will take a look.

Comment From: spring-cloud-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-cloud-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

Comment From: ariady-putra

2023 and this still happens

Comment From: OlgaMaciaszek

@ariady-putra please provide a minimal, complete, verifiable example that reproduces the issue.

Comment From: spring-cloud-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-cloud-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

Comment From: ulpcan

This problem continues

Client response: https://ibb.co/4JB2zTW Received empty body: https://ibb.co/Y3yKvTF Decoder: https://ibb.co/BPqS1pY

Spring boot: 3.1.2

Spring cloud version: 2022.0.4

Comment From: gokulkalagara

Hello, @ulpcan, have you found a solution for response body empty (401 error)?

Comment From: OlgaMaciaszek

@ulpcan if you want us to look at the issue, please provide a minimal, complete, verifiable example that reproduces the issue, as a link to a GitHub repo with a sample app or tests.

Comment From: ulpcan

It is very obvious that you get null body response when http code is 401. There is no further needed information.

Go write Feign configuration, send http request with it. If you get 401 from anywhere, anytime you will see at the debugger feign decoder reponse' is always null

Comment From: ulpcan

Hello, @ulpcan, have you found a solution for response body empty (401 error)?

Nope sorry

Comment From: gokulkalagara

To Resolve this empty response body for 401

Need to add extra dependency feign-httpclient

<dependency>
  <groupId>io.github.openfeign</groupId>
  <artifactId>feign-httpclient</artifactId>
</dependency>

When you are running in debug mode, while reading response body, if it throws an java.io.IOException: the stream is closed in ErrorDecoder

please disable toString() option in Intellij

Preferences | Build, Execution, Deployment | Debugger | Data Views | Java

Screenshot 2024-01-09 at 5 56 25 PM

** Suggested link (Switch to ApacheHttpClient)** https://stackoverflow.com/questions/43045332

Comment From: OlgaMaciaszek

This is done here, within the core Feign library implementation. This is not part of the Spring Cloud OpenFeign project, which just adds additional functionality and integrations on top of that third-party library. If you would like someone to look at this issue, please report it in the OpenFeign/feign repository.

Comment From: stefan-g

@gokulkalagara I'm facing the same problem that the response in the error decoder contains an empty body but the suggested solution does not work. My problem is that i'm using spring cloud Netflix and ApacheHttpClient doesn't seems to be able to automatically resolve the loadbalance url into the real url. any solution to fix this problem?

Comment From: OlgaMaciaszek

@stefan-g are you also facing the problem only in your docker environment or also locally?

Comment From: stefan-g

@OlgaMaciaszek currently i'm testing it locally (docker is not used). I'm getting an java.net.UnknownHostException because the service id is not resolved to the actual url (It does work when i don't define ApacheHttpClient as a bean).

i tried it with and without@LoadBalanced but with no difference.

  @Bean
  //@LoadBalanced
  public Client feignClient() {
    return new ApacheHttpClient();
  }

should the service id be resolved automatically?

Comment From: OlgaMaciaszek

@stefan-g No, we do not support @LoadBalanced Client. You will find info on all the available integrations for Spring Cloud LoadBalancer in the docs. For Feign Clients we integrate using @FeignClient annotation by creating our own Client implementation and setting it up under the hood - you can see more details here.

Comment From: stefan-g

@OlgaMaciaszek thanks for the links. that helped.

the following settings fix the problem for me:

add the following dependency at the gradle.build implementation 'io.github.openfeign:feign-hc5:13.3'

and enable the feign-hc5 client for spring cloud in the application.yml:

spring:
  cloud:
    openfeign:
      httpclient:
        hc5:
          enabled: true

Comment From: JerryngtonD

As a workaround for the issue where Feign response bodies are null on errors 401 etc, you can define a custom Feign client like this and apply it via configuration:

public class CustomFeignConfig {

@Bean
public Client feignClient() {
    return (request, options) -> {
        HttpURLConnection connection = (HttpURLConnection) new URL(request.url()).openConnection();
        connection.setRequestMethod(request.method());
        connection.setDoInput(true);
        connection.setDoOutput(request.body() != null);

        // Set request headers
        request.headers().forEach((key, values) -> connection.setRequestProperty(key, String.join(",", values)));

        // Write request body if present
        if (request.body() != null) {
            try (OutputStream os = connection.getOutputStream()) {
                os.write(request.body());
                os.flush();
            }
        }

        // Get status code and input stream (error or regular)
        int status = connection.getResponseCode();
        InputStream responseStream = (status >= 400) ? connection.getErrorStream() : connection.getInputStream();
        byte[] body = responseStream != null ? responseStream.readAllBytes() : new byte[0];

        // Collect response headers, excluding null keys
        Map<String, Collection<String>> headers = connection.getHeaderFields().entrySet().stream()
                .filter(e -> e.getKey() != null)
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        e -> new HashSet<>(e.getValue())
                ));

        // Build and return Feign response
        return Response.builder()
                .status(status)
                .reason(connection.getResponseMessage())
                .headers(headers)
                .body(body)
                .request(request)
                .build();
    };
}
@FeignClient(
        name = "anotherClient",
        url = "${server-url}",
        configuration = CustomFeignConfig.class

)
public interface FeignClient {