2

I am doing a REST Call to a Teamcity URI, to gte the lastSuccessful build Number but getting 406. If i use the same URI in Chrome's REST Console, i get the correct String ( Which is the latest Build Number

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

public class LastSuccessBuildNum {

    public static void main(String[] args) {

        try {

            Client client = Client.create();
            // client basic auth demonstration 
            client.addFilter(new HTTPBasicAuthFilter("username", "password"));

            WebResource webResource = client
                    .resource("http://localteamcity.com/teamcity/app/rest/buildTypes/id:bt26/builds/status:SUCCESS/number");
            ClientResponse response = webResource.accept("application/json")
                    .get(ClientResponse.class);

            if (response.getStatus() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatus());
            }

            String output = response.getEntity(String.class);

            System.out.println("Output from Server .... \n");
            System.out.println(output);
        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

stdout:

java.lang.RuntimeException: Failed : HTTP error code : 406
    at LastSuccessBuildNum.main(LastSuccessBuildNum.java:22)
kamal
  • 9,637
  • 30
  • 101
  • 168

1 Answers1

5

Check the MIME type of the transfer in Chrome REST Client, maybe it is not json. 406 means that the server does not have a MIME type that the client accepts: http://www.checkupdown.com/status/E406.html

Is there a specific reason that you use jersey client instead of Apache Http Components?

Elchin
  • 584
  • 6
  • 19
  • json is available according to WADL, also ,even if i change to webResource.accept("application/text") i still get 406 – kamal Aug 10 '12 at 14:33
  • Can you open Network tab in Developer Tools in Chrome REST client and see the transferred MIME types there? – Elchin Aug 10 '12 at 14:34
  • could it be: Resource interpreted as Image but transferred with MIME type text/javascript: "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js". sf_main.jsp:25 Resource interpreted as Script but transferred with MIME type text/plain: "http://www.superfish.com/ws/getSupportedSitesJSON.action?ver=5.6&callback=SF_isURISupported". script.xd.js:8 Resource interpreted as Script but transferred with MIME type text/plain: "http://www.superfish.com/ws/rvwl.action?ver=3&callback=SF_isRvURISupported". script.xd.js:8 – kamal Aug 10 '12 at 14:45
  • 2
    @kamal there is no MIME tyoe `application/text` try `text/plain` instead. – Alex Stybaev Aug 10 '12 at 14:51
  • Bingo Thanks @AlexStybaev that did the trick. I wonder why mkyong ref http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/ has webResource.type("application/json") – kamal Aug 10 '12 at 14:53
  • AFAIK, json was added later to MIME types, and added as an applicaton/ instead of text/, cause it was considered to be application-specific. – Elchin Aug 10 '12 at 15:00
  • http://stackoverflow.com/questions/477816/what-format-should-i-use-for-the-right-json-content-type – kamal Aug 10 '12 at 15:23