Start a process instance using REST

cancel
Showing results for 
Search instead for 
Did you mean: 
signjoy
Active Member II

Start a process instance using REST

Jump to solution

I have Springboot app that talks to Alfresco Enterprise Activiti using REST calls.

I want to start the process instance using REST call but I am getting 400 bed request. however the same request works with postman.

String url = "http://localhost:9091/activiti-app/api/enterprise/process-instances";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("admin@app.activiti.com", "admin"));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
CreateProcessInstanceRepresentation cpir = new CreateProcessInstanceRepresentation ();
cpir.setName("ClientReview");
cpir.setProcessDefinitionId("ClientReview:7:15103");
ObjectMapper mapper = mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
json = mapper.writeValueAsString(cpir);
JsonNode jsonNode = mapper.readTree(json);

restTemplate.exchange(url, HttpMethod.POST, entity, Object.class, jsonNode); // Throws 400 Bad Request

Please help..
Thank you in advance...






1 Solution

Accepted Solutions
naveenv449
Active Member II

Re: Start a process instance using REST

Jump to solution

Hi,

Please find below some working code snippet for the starting of Activiti Process instance using REST.

Code:

String urlString = "http://localhost:8080/activiti-app/api/enterprise/process-instances";
String propertiesJson = ""; //If you have any form fields to start the form
PostMethod mPost = null;
HttpClient client = new HttpClient();
mPost = new PostMethod(urlString);
String encoding = Base64.getEncoder().encodeToString("admin@app.activiti.com:admin".getBytes());
mPost.setRequestHeader("Authorization", "Basic " + encoding);
mPost.setRequestHeader("Content-Type", "application/json");
mPost.setRequestEntity(new ByteArrayRequestEntity(propertiesJson.getBytes()));
int statusCode = client.executeMethod(mPost);
System.out.println("Response Code:::"+statusCode);

===========================================

Imports:

import org.apache.commons.httpclient.HttpClient;
import java.util.Base64;
import org.apache.commons.httpclient.methods.PostMethod;

Hope this helps.

Thanks,

Naveen

View solution in original post

1 Reply
naveenv449
Active Member II

Re: Start a process instance using REST

Jump to solution

Hi,

Please find below some working code snippet for the starting of Activiti Process instance using REST.

Code:

String urlString = "http://localhost:8080/activiti-app/api/enterprise/process-instances";
String propertiesJson = ""; //If you have any form fields to start the form
PostMethod mPost = null;
HttpClient client = new HttpClient();
mPost = new PostMethod(urlString);
String encoding = Base64.getEncoder().encodeToString("admin@app.activiti.com:admin".getBytes());
mPost.setRequestHeader("Authorization", "Basic " + encoding);
mPost.setRequestHeader("Content-Type", "application/json");
mPost.setRequestEntity(new ByteArrayRequestEntity(propertiesJson.getBytes()));
int statusCode = client.executeMethod(mPost);
System.out.println("Response Code:::"+statusCode);

===========================================

Imports:

import org.apache.commons.httpclient.HttpClient;
import java.util.Base64;
import org.apache.commons.httpclient.methods.PostMethod;

Hope this helps.

Thanks,

Naveen