Visa Developer Community

Highlighted
Regular Visitor

Can't read response body (already consumed?)

I am just getting started using Visa Developer. I created project using the Foreign Exchange Rates API. I downloaded the sample code for my project and got it to work.

 

I am now creating my own application, based on the sample project. I am able to make the request using the VisaAPIClient class, and am able to check the response status code, as in the sample code. However, I am having trouble reading the response body (which the sample code does not do).

 

Here is my code:

 

        CloseableHttpResponse response = visaAPIClient.doMutualAuthRequest(baseUri + resourcePath, "Foreign Exchange Test", foreignExchangeRequest, MethodTypes.POST, new HashMap<String, String>());
       
        int responseStatus = response.getStatusLine().getStatusCode();
        if (responseStatus != HttpStatus.SC_OK)
        {
        	throw new Exception("Visa API request failed: " + responseStatus);
        }
        
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String responseBody = reader.readLine();
        
        response.close();

On the call to reader.readLine(), I get the following exception:

Exception in thread "main" java.io.IOException: Attempted read from closed stream.
	at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:167)
	at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:137)
	at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
	at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
	at sun.nio.cs.StreamDecoder.read(Unknown Source)
	at java.io.InputStreamReader.read(Unknown Source)
	at java.io.BufferedReader.fill(Unknown Source)
	at java.io.BufferedReader.readLine(Unknown Source)
	at java.io.BufferedReader.readLine(Unknown Source)
	at DCCMain.postRequestVisa(DCCMain.java:70)
	at DCCMain.main(DCCMain.java:107)

In researching this error, it appears that it is due to the response entity being already consumed, and the fact that you can only read the stream once. I noticed that in the VisaAPIClient class, the logResponse() method is indeed consuming the response body. I tested this my commenting out the body of that method, and now my code works, and I am able to retrieve the response body.

 

So, what should I be doing to correctly retrieve the body of the response. If logResponse is already consuming it before I can get to it, how am I supposed to get it myself?

 

-Joe

 

1 REPLY
Regular Contributor

Re: Can't read response body (already consumed?)

Hi Joe,

Thanks for your question and thanks for highlighting this!

 

Visa has provided these sample code in very basic level to provide some insight, demonstrate best practices, and as a quick starting point for developers. Therefore this is not 100% production implementation ready and we expect client developers to customize the code to integrate with their backend/downstream applications based on their requirements and use cases.

 

For your case, you may first consume the response and then use a copy of the response to log it; as you have already done.

 

Further, when you log the response you might have to suppress/obfuscate any sensitive data, such as PAN information, when use with certain Visa Direct APIs. These functionalities are not included in the sample code. Therefore we expect client developers to enhance/modify the functionalities.

 

Hope above clarification helps. Should you have further concerns kindly let us know.

 

Thanks

Lakshan