We are finally migrating to 15.1, and have some code in a standalone client app that is affected by this change in http library. We have a List in LabKey in which one field is of type File; we manually upload a text file to this field when we insert a new record to this List. Our app that uses the LabKey client API and the http library can calculate the URL to fetch this file, and wants to read its contents.
Our code that works with LabKey 14.3 and HttpClient 3.x is the following:
GetMethod get = new GetMethod( urlString );
labkeyConnection.executeMethod( get );
String fileContents = get.getResponseBodyAsString();
where urlString is the URL as a String, labkeyConnection is a org.labkey.remoteapi.Connection, properly authenticated via username and password.
I think I figured out most of how to re-write this using the new API - except for authentication. Here is what I have so far (without error/exception handling):
CloseableHttpClient httpClient = labkeyConnection.getHttpClient( 60 );
HttpGet get = new HttpGet( urlString );
CloseableHttpResponse response = httpClient.execute( get );
HttpEntity entity = response.getEntity();
String fileContents = EntityUtils.toString( httpEntity );
When I execute this, the fileContents appears to be the login page, rather the file I want. I started looking at the docs for how to do authentication with the new HttpClient APIs here:
https://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html and it seems like rather a lot of code (CredentialsProvider, AuthCache, BasicScheme, HttpClientContext, etc.), and I'm wondering why I have to re-authenticate, if I'm using the CloseableHttpClient object gotten from the LabKey Connection object - to which I've already provided host, port, username and password.
Am I doing something wrong? Is there an easier/different way to do this with the new APIs?
Thanks.
Andy Straw
University of Rochester