0

I normally use cUrl to hit my endpoint at the minute. But am not at the stage where I need to think about building the client.

I normally hit my endpoint the following way :

curl -u "adminaccount:adminpassword" http://localhost:8080/api/private/v1/endpoint/

Lets say I have the below code

 HttpGet getRequest = new HttpGet(
        "http://localhost:8080/api/private/v1/endpoint/");

 HttpResponse response = httpClient.execute(getRequest);

Now, I obviously need to pass in the equivalent of that :

-u" adminaccount:adminpassword"

somehow. Any ideas?

MickeyThreeSheds
  • 986
  • 4
  • 23
  • 42
  • 1
    If you are going to downvote, at least leave a comment. – Edward Feb 22 '17 at 23:22
  • Take a look at the HttpClient docs: https://hc.apache.org/httpcomponents-client-ga/examples.html – Jyr Feb 22 '17 at 23:29
  • How about to google what HTTP Authorization header is and how to set it in HttpGet? -- hint: in your case it is about BASIC authentication schema... – Vadim Feb 22 '17 at 23:31
  • Love this kind of "Why don't you google" comment - when I am clearly in a position where I don't even know what to be googling. Have you never had a problem you are completely new to before? – MickeyThreeSheds Feb 22 '17 at 23:35
  • @MickeyThreeSheds you could start by googling for "what is the u option in curl", find this (http://stackoverflow.com/questions/20737031/curlss-option-u) as the first result, and thus know how to go next. – JB Nizet Feb 23 '17 at 00:11
  • I did that, actually - then I found the credentials provider stuff which was also linked above - But when I set my credentials provider up, and add it to the default http client - then do the get code above, it returns a 406. Which means they must not be the same deal. – MickeyThreeSheds Feb 23 '17 at 00:13
  • Then why don't you tell all that in your question, posting your code, and posting the error response you got? An error 406 has nothing to do with authentication. You would normally get a 401 response for a bad authentication. If you use google to find out what a 406 status code means, you'll find this: https://httpstatuses.com/406. You need to send Accept headers specifying what you accept, and make sure the content-type of the response produced by the server is one of the accepted content types sent in the Accept header. Google for "how to print request sent by curl". – JB Nizet Feb 23 '17 at 00:26
  • 1
    Jesus you are all nice and toxic, you would nearly think I asked this question in a ruby on rails site. – MickeyThreeSheds Feb 23 '17 at 05:08
  • I don't see anything wrong with this question. It is only asking how curl uses '--user' option in its HTTP requests, which I think is a valid question to ask. – Anurag Mar 30 '20 at 04:02

1 Answers1

0

When creating your HttpClient:

CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope("localhost", 8080),
                new UsernamePasswordCredentials("adminaccount", "adminpassword"));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider)
                .build();

But yeah instead of getting in a huff about being told to google it, just go google it. Start with googling for "HttpClient authentication example" for example. The rules of the site are quite clear - "Do your homework first"

  • I did google this actually, but it isn't the same as curl -u, because curl -u works, and this method returns a http response of 406. – MickeyThreeSheds Feb 23 '17 at 00:14