1

I am receiving an error org.apache.http.client.HttpResponseException: Bad Request when I try to run this program.

Could you please help me understand where I should modify the code ?

I am using the following libraries httpclient-4.4.1.jar httpcore-4.4.1.jar commons-logging-1.1.2.jar

org.apache.http.client.HttpResponseException: Bad Request

Here is the code :

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class Test {
public static void main(String args[]) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost("http://localhost:8080/engine-rest/process-definition/key/demo-scaling/start");
    try {
        StringEntity input = new StringEntity("(\"variables\":{}, \"businessKey\" : \"AAA001\")");
        postRequest.addHeader("Accept", "application/json");
        postRequest.setEntity(input);
        postRequest.addHeader("Content-Type", "application/json");
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(postRequest, responseHandler);
        System.out.println(responseBody);
    } catch (Exception e) {

        e.printStackTrace();
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

}
4
  • "localhost:8080/engine-rest/process-definition/key/demo- scaling/start" why you have a spaces there before scaling/start? If you need there spaces, use %20 for that. THen you will have a valid url and right request :) Commented Jun 24, 2016 at 14:45
  • I have it correct in the program Hrabosch where I just checked it now. Commented Jun 24, 2016 at 14:55
  • Ok then you can try for example Postman extension for Chrome and try to call i manually. Commented Jun 24, 2016 at 14:57
  • From the postman its actually working but I have a requirement to call it via Java code Commented Jun 24, 2016 at 15:13

1 Answer 1

1

Ok, try this:

 HttpClient httpClient = HttpClientBuilder.create().build();

    try {
        HttpPost request = new HttpPost("http://localhost:8080/engine-rest/process-definition/key/demo-scaling/start");
        StringEntity params =new StringEntity("variables={\"businessKey\":\"AAA001\"}");
        request.addHeader("content-type", "application/x-www-form-urlencoded");
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);

        System.out.println(response);
    }catch (Exception ex) {
        // handle Exceptions
    }

Use httpclientbuilder to get client (DefaultHttpClient -> Deprecated) and i am not sure if you have a valid JSON data, this is only my suggest.

Sign up to request clarification or add additional context in comments.

1 Comment

if we want GET request is that just change the POST into GET and remove the parameters...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.