how to call HTTP API from java?

275 views
Skip to first unread message

amqp...@gmail.com

unread,
Jan 14, 2016, 9:37:14 PM1/14/16
to rabbitmq-users

I have a requirement to create a virtual host from java client. What I did:

1) Created a new 'admin' user from rabbitmq console, logged in with admin user to verify.

2) created a json file with content:
{
  "rabbit_version": "3.4.3",
  "vhosts": [
    {
      "name": "test"
    }
  ],
  "permissions": [
    {
      "user": "admin",
      "vhost": "test",
      "configure": ".*",
      "write": ".*",
      "read": ".*"
    }
  ]
}

3) From a java class, tried to run the http api

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 15672),new UsernamePasswordCredentials("admin", "adminpasswd"));
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("content-type", "application/json");
JSONObject param = new JSONObject();
String restTestJson = FileUtils.readFileToString(new ClassPathResource("test.json").getFile()); 
headers.add("data", restTestJson); 
StringEntity entity = new StringEntity(param.toString());
httpPost.setEntity(entity);
HttpResponse response = httpclient.execute(httpPost);

ERROR: 
Caused by: org.apache.http.NoHttpResponseException: localhost:15672 failed to respond
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:283)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:251)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:197)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:685)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:487)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106) 

From the stack, it seems that the error is nothing to do with rabbitmq.... but I am looking for any pointers what I am doing wrong. I did not find any example using java.

It works fine if I run:
  a) curl
     curl -u admin:adminpasswd -vX POST http://localhost:15672/api/definitions -d @/test.json --header "Content-Type: application/json"
  
  b) from console using import --> Update Broker Definitions.

4) Also tried using rest template but no luck:
            String restTestJson = FileUtils.readFileToString(new ClassPathResource("test.json").getFile());
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> entity = new HttpEntity<String>(restTestJson,headers);

            
            String url = "http://localhost:15672/api/definitions";
            RestClient restClient = new RestClient("admin", "admin");
            restClient.postForObject(url, entity, String.class);
restclient is from here:

       => same error:
org.apache.http.NoHttpResponseException: localhost:15672 failed to respond
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:161)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:153)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:254)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:91)
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:596)
             

amqp...@gmail.com

unread,
Jan 14, 2016, 10:28:23 PM1/14/16
to rabbitmq-users
worked fine now with basic auth:
    String plainCreds = "admin:adminpassword";
            byte[] plainCredsBytes = plainCreds.getBytes();
            byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
            String base64Creds = new String(base64CredsBytes);

            HttpHeaders headers = new HttpHeaders();
            headers.add("Authorization", "Basic " + base64Creds);
            headers.setContentType(MediaType.APPLICATION_JSON);

Michael Klishin

unread,
Jan 15, 2016, 6:02:46 AM1/15/16
to rabbitmq-users
Have you considered saving yourself some effort and using Hop?

Reply all
Reply to author
Forward
0 new messages