HTTP API request 401 Unauthorized

751 views
Skip to first unread message

Gordana Jekic

unread,
Apr 20, 2015, 5:36:59 AM4/20/15
to rabbitm...@googlegroups.com

I'm trying to access to RabbitMQ rest, but I got 401 unauthorized error. I want to access to queue information and to get messages number.

I found this as a solution

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("xx.xx.xx.xx", 15672, "http");

HttpPut request = new HttpPut(
    "/api/queues/%2F/queue-name");

httpClient.getCredentialsProvider().setCredentials(
    new AuthScope(targetHost.getHostName(), targetHost.getPort()),
    new UsernamePasswordCredentials("guest", "guest"));

AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

request.addHeader("Content-Type", "application/json");

StringEntity input = new StringEntity(
    "{\"vhost\":\"/\",\"durable\":\"false\",\"auto_delete\":\"false\",\"arguments\":{}}");

request.setEntity(input);

HttpResponse response = httpClient.execute(targetHost, request, localcontext);

but it doesn't work. I saw that DefaultHttpClient class is depreciated so I tried something like this

HttpHost targetHost = new HttpHost("xx.xx.xx.xx", 15672, "http");
    HttpPut request = new HttpPut("/api/whoami");

    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    CredentialsProvider credentialProvider = new BasicCredentialsProvider();
    credentialProvider.setCredentials(
        new AuthScope(targetHost.getHostName(), targetHost.getPort()),
        new UsernamePasswordCredentials("guest","guest")
    );

    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credentialProvider);
    context.setAuthCache(authCache);

    request.addHeader("Content-Type", "application/json");

    HttpResponse response = httpClient.execute(targetHost, request, context);

and then to access to REST thought WebTarget, something like this

WebTarget queueREST = RESTClientManager.getClient().target("xx.xx.xx.xx:15672/api/queues/%2F/queue-name");

but I still get error 401 Unauthorized. Any suggestion how to solve this problem?


--
The content of this e-mail, together with any of its attachments, is for the exclusive and confidential use of the named addressee(s) and it may contain legally privileged and confidential information and/or copyrighted material. Any other distribution, use or reproduction without the sender's prior consent is unauthorized and strictly prohibited. If you have by coincidence, mistake or without specific authorization received this e-mail in error, please notify the sender by e-mail immediately, uphold strict confidentiality and neither read, copy, transfer, disseminate, disclose nor otherwise make use of its content in any way and delete the material from your computer.

The content of the e-mail and its attachments is the liability of the individual sender, if it does not relate to the affairs of Betware.
Betware does not assume any civil or criminal liability should the e-mail or it´s attachments be virus infected.

Michael Klishin

unread,
Apr 20, 2015, 5:40:13 AM4/20/15
to Gordana Jekic, rabbitm...@googlegroups.com
On 20 April 2015 at 12:37:01, Gordana Jekic (gje...@betware.com) wrote:
> I still get error 401 Unauthorized. Any suggestion how to solve
> this problem?

Yes. Use Hop [1] and don't reinvent the wheel. While it has little docs, the API is very straightforward
and it solves a couple of HTTP authentication edge cases with the Apache HTTP client for you. 

1. http://github.com/rabbitmq/hop
--
MK

Staff Software Engineer, Pivotal/RabbitMQ


Gordana Jekic

unread,
Apr 20, 2015, 8:19:52 AM4/20/15
to rabbitm...@googlegroups.com, gje...@betware.com
Ok, I implement this, but which function to use? c.getConnections(); ?

Michael Klishin

unread,
Apr 20, 2015, 10:00:47 AM4/20/15
to Gordana Jekic, rabbitm...@googlegroups.com
On 20 April 2015 at 15:19:54, Gordana Jekic (gje...@betware.com) wrote:
> Ok, I implement this, but which function to use? c.getConnections();
> ?

getConnections is for GET /api/connections.

You want to use

PUT /api/queues/%2F/queue-name

then you probably want Client#declareQueue(vhost, name, queueInfo).

You may want to get familiar with the test suite:
https://github.com/rabbitmq/hop/blob/master/src/test/groovy/com/rabbitmq/http/client/ClientSpec.groovy 

Gordana Jekic

unread,
Apr 20, 2015, 10:17:53 AM4/20/15
to rabbitm...@googlegroups.com, gje...@betware.com
Thank you very much, I found everything what I needed. 

Reply all
Reply to author
Forward
0 new messages