OpenFeign / OpenFeign/feign

Unable to print Response as String without corrupting the response.

Open
#1,187 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feedback provided
Dominant language
Java
Stars
9.8k
Forks
1.9k
Avg merge
1d 2h
Merged PRs (30d)
41

Description

I am trying to write an aspect for all feign calls that we use in our microservices. we use feign in almost all our microservices. I am trying to log the request and response of all the feign calls. The code is below:
@Slf4j
@Configuration
@Aspect
@Order(0)
public class FeignRequestResponse {

@Pointcut("execution(public * org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient.execute(..))")
public void pointCutFeign() {
}
@Around("pointCutFeign()")
public Object myAroundFeign(ProceedingJoinPoint joinPoint) throws Throwable {
    int uniqueIdentifier = generateUniqueIdentifier(10000000,0);
    if (joinPoint.getArgs().length > 0) {
        Request request = (Request) joinPoint.getArgs()[0];
        log.info("THE PICKUP SERVICE REQUEST IDENTIFIER  IS  {} ,THE PICKUP REQUEST URL IS {}, THE PICKUP REQUEST METHOD IS  {},THE PICKUP REQUEST BODY IS {}", uniqueIdentifier,request.url(),request.httpMethod().name(),request.requestBody().asString());
   
    }
    Response response = (Response) joinPoint.proceed();
           log.info("THE PICKUP SERVICE RESPONSE ID = {},PICKUP RESPONSE STATUS = {},PICKUP RESPONSE BODY = {}", uniqueIdentifier,response.status(),IOUtils.toString(response.body.asInputStream());

    return response;
}

The problem here is for above codeIOUtils.toString(response.body.asInputStream() is getting response displayed properly as String but then response object is getting mangled so due to that response object is becoming null and hence all the api's output is shown as null.

Is there a way where we can display Response object's body as String and also return Response object without disturbing it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the FeignRequestResponse aspect shown in the issue, especially joinPoint.proceed() and response.body.asInputStream(). Read the Feign Response body handling to understand why reading the stream affects the returned response. Done means the body can be logged as text while the returned API response remains intact; no repository file or test is named.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.