watson-developer-cloud / java-sdk

:1st_place_medal: Java SDK to use the IBM Watson services.
http://watson-developer-cloud.github.io/java-sdk/
Apache License 2.0
590 stars 533 forks source link

HttpLoggingInterceptor logging level may cause an exception when uploading files #1018

Closed zlogic closed 5 years ago

zlogic commented 5 years ago

It appears that increasing the log level may cause issues with uploading files.

How to reproduce:

  1. set the com.ibm.watson.developer_cloud.util.HttpLogging#LOG level to ALL
  2. Call a Watson service with which uploads a file, for example the Visual Recognition service:
    
    IamOptions options = new IamOptions.Builder()
    .apiKey("{apikey}")
    .build();

VisualRecognition service = new VisualRecognition("2018-03-19", options);

DetectFacesOptions detectFacesOptions = new DetectFacesOptions.Builder() .imagesFile(new File("./Ginni_Rometty.jpg")) .build(); DetectedFaces result = service.detectFaces(detectFacesOptions).execute(); System.out.println(result);



`HttpLoggingInterceptor` logs the request body by calling `com.ibm.watson.developer_cloud.http.InputStreamRequestBody#writeTo`, and then the actual upload is done by calling `writeTo` a second time for the same `InputStream`.

The second `writeTo` call (the one which actually does the real upload) fails because the stream is already closed.
lpatino10 commented 5 years ago

Hey @zlogic, thanks a ton for finding this issue. I was just able to reproduce and can verify that it doesn't work as intended.

I'll start taking a look at a fix.