0

I am trying to reach API via https using username and password with Java and Apache HttpsClient (my framework is based on it). I have access to curl command that works and python code. Both are resolved in one line. The Python code:

request.post(url=URL, files={'mpg': open(file_name, 'rb')}, auth=(r'user', r'passwrod'),verify=True)

The curl command:

curl -u 'user:password' -v -F 'mpg=@file.m4a' https://address

Of the ways I tried in Apache library is:

URIBuilder

 URIBuilder uri = new URIBuilder();
    uri.setScheme("https").setHost("host").setPath("path").setUserInfo("user","password");  

SSLContextBuilder (with credentials provider)

        SSLContextBuilder builder = new SSLContextBuilder();
   builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());

CredentialsProvider

   CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
                new UsernamePasswordCredentials("user", "password"));
httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();

Also multiple variations of the above. I am getting increasingly frustrated as to how long it takes me to try to figure it out since outside of java it can be handled in one line of code. The file submit part works as I was able to run it before authorization and https implemented.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
Ver Siw
  • 11
  • 2

1 Answers1

0

I was able to find the answer after all. the code hat actually works is:

        request.addHeader("Authorization", "Basic "+ base64.getEncoder().encodeToString("user:pasword".getBytes()));

Related post that helped to find the soultion: Apache HTTPClient POSTs to REST service differently than cURL

Ver Siw
  • 11
  • 2