2

I am trying to create a push topic in java by following this link . Since there were changes in the eclipse httpclient library (I am using version 9.2.5) I had to make corresponding changes and the code now looks as below. While making the http post request for creating the push request I get the response as "HttpResponse[HTTP/1.1 400 Bad Request]@66fa8363"

private static final String API_VERSION = "23.0";

    public static void main(String[] args) throws Exception {
        if (args.length < 2) {
            System.err.println("Usage: MakeTopic topicname query");
            System.exit(-1);
        }

        String topicname = args[0];
        String query = args[1];

        HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP(), new SslContextFactory(true));
        httpClient.start();

        JSONObject authResponse = oauthLogin(httpClient);
        System.out.println("Login response: " + authResponse.toString(2));
        if (!authResponse.has("access_token")) {
            throw new Exception("OAuth failed: " + authResponse.toString());
        }

        String url = authResponse.getString("instance_url")
                + "/services/data/v23.0/sobjects/PushTopic/";

        JSONObject topic = new JSONObject();

        topic.put("ApiVersion", API_VERSION);
        topic.put("Name", topicname);
        topic.put("Query", query);

        System.out.print("PushTopic data: ");
        System.out.println(topic.toString(2));

        System.out.println("Push URL : " + url);
        System.out.println("Topic : " + topic.toString());
        httpClient.POST(url).header(HttpHeader.CONTENT_TYPE, "application/json")
                .header(HttpHeader.AUTHORIZATION, "OAuth " + authResponse.getString("access_token"))
                .content(new StringContentProvider(topic.toString())).send(new Response.CompleteListener() {
            @Override
            public void onComplete(Result result) {
                System.out.println("Result status : " + result.isSucceeded());
                System.out.printf("Response : " + result.getResponse()); //                System.out.println(result.getResponse().getReason()); //               result.getFailure().printStackTrace();
            }
        });

        while (true) {
            TimeUnit.MINUTES.sleep(5);
        }
    }

    private static JSONObject oauthLogin(HttpClient httpClient) throws Exception {
        String url = LOGIN_SERVER + "/services/oauth2/token";
        String message = "grant_type=password&client_id=" + CLIENT_ID
                + "&client_secret=" + CLIENT_SECRET + "&username=" + USERNAME
                + "&password=" + PASSWORD;


        ContentResponse contentResponse = httpClient.POST(url).header(HttpHeader.CONTENT_TYPE, "application/x-www-form-urlencoded")
                .content(new StringContentProvider(message)).send();

        return new JSONObject(new JSONTokener(contentResponse.getContentAsString()));
    }

The command output of the above execution is

2014-11-28 11:42:45.579:INFO::main: Logging initialized @70ms
Login response: {
  "id": "https://login.salesforce.com/id/00D90000000mOWsEAM/00590000001XDqBAAW",
  "issued_at": "1417155166607",
  "instance_url": "https://ap1.salesforce.com",
  "token_type": "Bearer",
  "access_token": "00D90000000mOWs!AQkAQDtcskg4wEH5yMMOPnG1TWLx9ENr8zaa9xc.wyoq8pCDKuMgYjQD9Hy_d3kep8EssxPTRsDssS.0a7K2GTxq9NCuV6eE",
  "signature": "Nlut7j39pc8rsvFjAVVoQIHU6f7oUkbe9YKlFbyYt6s="
}
PushTopic data: {
  "Name": "TestTableTopic",
  "Query": "SELECT Id, Id__c FROM TestTable__c",
  "ApiVersion": "23.0"
}
Push URL : https://ap1.salesforce.com/services/data/v23.0/sobjects/PushTopic/
Topic : {"Name":"TestTableTopic","Query":"SELECT Id, Id__c FROM TestTable__c","ApiVersion":"23.0"}
Result status : true
Response : HttpResponse[HTTP/1.1 400 Bad Request]@66fa8363

Unfortunately I can't find a way to print out more details on http response. What could be going wrong?

  • Is there a response body with the 400? If present it would hold more clues as to the issue. – Daniel Ballinger Nov 28 '14 at 17:57
0

By using a different REST API client I figured out that the root cause of the failure was "duplicate topic creation". Salesforce didn't allow creating a topic with the same name. Since the client response in java didn't have the exact details it took a while to do that.

A successful topic creation returns with http status code 201. So that should help in knowing if the operation was successful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.