0

I am following the problem, when using restTemplate.exchange with http GET verb

I'm making a request to wait for the following json

{
    "responseCode": 200,
    "message": "success",
    "content": {
        "id": "201284561879666",
        "username": "edersongervasiosilva",
        "title": "Software Survey Form",
        "height": "0",
        "status": "ENABLED",
        "created_at": "2020-05-08 10:26:16",
        "updated_at": "2020-05-10 19:01:13",
        "last_submission": null,
        "new": "0",
        "count": "0",
        "type": "CARD",
        "url": "https://form.jotform.com/201284561879666"
    },
    "duration": "13ms",
    "limit-left": 969
}

My call is:

public void getForm() {

        restTemplate.setInterceptors(Collections.singletonList(new RequestResponseLoggingInterceptor()) );
        HttpHeaders headers = new HttpHeaders();
        headers.add("user-agent", "");
        headers.set(API_KEY, getApiKey());
        HttpEntity httpEntity = new HttpEntity(headers);
        ResponseEntity<Object> response = restTemplate.exchange(getUrl()+"201284561879666", GET, httpEntity, Object.class);
        System.out.println("Body " + response.getBody());
    }

response.getBody() is NULL

In the interceptor that I registered the response is correct, but I don't know why it is null in the ResponseEntity

Interceptor:

@Slf4j
public class RequestResponseLoggingInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException
    {
        logRequest(request, body);
        ClientHttpResponse response = execution.execute(request, body);
        logResponse(response);

        //Add optional additional headers
        response.getHeaders().add("headerName", "VALUE");

        return response;
    }

    private void logRequest(HttpRequest request, byte[] body) throws IOException
    {
        if (true)
        {
            log.info("===========================request begin================================================");
            log.info("URI         : {} " + request.getURI());
            log.info("Method      : {} " + request.getMethod());
            log.info("Headers     : {} " + request.getHeaders());
            log.info("Request body: {} " + new String(body, "UTF-8"));
            log.info("==========================request end================================================");
        }
    }

    private void logResponse(ClientHttpResponse response) throws IOException
    {
            log.info("============================response begin==========================================");
            log.info("Status code  : {} " + response.getStatusCode());
            log.info("Status text  : {} " + response.getStatusText());
            log.info("Headers      : {} " + response.getHeaders());
            log.info("Response body: {} " + StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
            log.info("=======================response end=================================================");
    }
}

LOG interceptor

===========================request begin================================================

URI         : {} https://api.jotform.com/form/201284561879666
Method      : {} GET
Headers     : {} {Accept=[application/json, application/*+json], user-agent=[PostmanRuntime/7.24.1], apiKey=[22b389c55ddf1ea86bb6b5f1ed93e3a4], Content-Length=[0]}
Request body: {} 

============================response begin==========================================

Status code  : {} 200
Status text  : {} OK
Headers      : {} {Date=[Mon, 11 May 2020 18:03:22 GMT], Content-Type=[application/json], Transfer-Encoding=[chunked], Connection=[keep-alive], Set-Cookie=[__cfduid=d29f878c66b00c3e5a21d45eab115889d1589220202; expires=Wed, 10-Jun-20 18:03:22 GMT; path=/; domain=.jotform.com; HttpOnly; SameSite=Lax], Vary=[Accept-Encoding], P3P=[CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"], Expires=[Thu, 01 Jan 1970 00:00:01 GMT], Last-Modified=[Mon, 11 May 2020 18:03:22 GMT], Cache-Control=[no-cache], Pragma=[no-cache], Access-Control-Allow-Origin=[*], Access-Control-Allow-Methods=[PUT, POST, GET, OPTIONS, DELETE], X-Form-Cache=[MISS], Via=[1.1 google], Alt-Svc=[h3-27=":443"; ma=86400, h3-25=":443"; ma=86400, h3-24=":443"; ma=86400, h3-23=":443"; ma=86400], CF-Cache-Status=[DYNAMIC], Expect-CT=[max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"], Server=[cloudflare], CF-RAY=[591dd0f6ac61f6cb-GRU], cf-request-id=[02a680ee2b0000f6cb9eaeb200000001]}
Response body: {} {"responseCode":200,"message":"success","content":{"id":"201284561879666","username":"edersongervasiosilva","title":"Software Survey Form","height":"0","status":"ENABLED","created_at":"2020-05-08 10:26:16","updated_at":"2020-05-10 19:01:13","last_submission":null,"new":"0","count":"0","type":"CARD","url":"https:\/\/form.jotform.com\/201284561879666"},"duration":"13ms","limit-left":963}

You can see that in response begin of the interceptor the response is filled

Ger
  • 583
  • 2
  • 13
  • 35

0 Answers0