cancel
Showing results for 
Search instead for 
Did you mean: 

REST API : Start an instance providing the key (not the id)

othmann
Champ in-the-making
Champ in-the-making
Hi everyone,

  First of all, congratulations for the nice work behind Activiti.

  I need to use the REST API to start a process instance. I chose to use the process definition key instead of the process definition id, so as to start automatically the latest version of the process. It is made clear in the user guide that we can make a POST request on /process-instance with :


{
  "processDefinitionKey":"financialReport:1",
  "businessKey":"order-4711"
}

   However, the engine keeps telling me :

{
    "status" :
  {
    "code" : 400,
    "name" : "Bad Request",
    "description" : "Request sent by the client was syntactically incorrect."
  }, 
 
  "message" : "02230006 Parameter 'processDefinitionId' is missing", 
  "exception" : "org.springframework.extensions.webscripts.WebScriptException - 02230006 Parameter 'processDefinitionId' is missing",
 
  "callstack" :
  [
       ""      ,"org.springframework.extensions.webscripts.WebScriptException: 02230006 Parameter 'processDefinitionId' is missing"
      ,"org.activiti.rest.util.ActivitiRequest.checkObject(ActivitiRequest.java:383)"
      ,"org.activiti.rest.util.ActivitiRequest.checkString(ActivitiRequest.java:367)"
      ,"org.activiti.rest.util.ActivitiRequest.getMandatoryString(ActivitiRequest.java:291)"
      ,"org.activiti.rest.api.process.ProcessInstancePost.executeWebScript(ProcessInstancePost.java:45)"
      ,"org.activiti.rest.util.ActivitiWebScript.executeImpl(ActivitiWebScript.java:68)"
      ,"org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:64)"
      ,"org.springframework.extensions.webscripts.PresentationContainer.executeScript(PresentationContainer.java:69)"
      ,"org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:333)"
      ,"org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:189)"
      ,"org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:118)"
      ,"javax.servlet.http.HttpServlet.service(HttpServlet.java:717)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)"
      ,"org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)"
      ,"org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)"
      ,"org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)"
      ,"org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)"
      ,"org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)"
      ,"org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)"
      ,"org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)"
      ,"org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)"
      ,"org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)"
      ,"java.lang.Thread.run(Thread.java:662)"

  ],
 
  "server" : "Spring WebScripts - v1.0.0 (Release Candidate 2 660) schema 1,000",
  "time" : "23-Mar-2011 15:59:36"
}

  When I use the processDefinitionId attribute, it works fine. Any idea ?

Thanks,
Othman
7 REPLIES 7

frederikherema1
Star Contributor
Star Contributor
Strange, the code does actually check if the processDefinitionKey is available. If not, the ID is mandatory:

org.activiti.rest.api.process.ProcessInstancePost:41

    String processDefinitionKey = req.getString("processDefinitionKey");
    String processDefinitionId = null;
    if (processDefinitionKey==null) {
      processDefinitionId = req.getMandatoryString(obj, "processDefinitionId");
    }

Are you sure you typed "processDefinitionKey" correct.?

hrabe
Champ in-the-making
Champ in-the-making
The problem could be

String processDefinitionKey = req.getString("processDefinitionKey");

public String getString(String param) {
    return checkString(req.getParameter(param), param, false);
}
is called on ActivitiRequest

whereas


processDefinitionId = req.getMandatoryString(obj, "processDefinitionId");

public String getMandatoryString(ActivitiRequestObject obj, String param) {
    return checkString(obj.getString(param), param, true);
}

is called on ActivitiRequestObject

othmann
Champ in-the-making
Champ in-the-making
Yes, it works when you give processDefinitionKey as a POST parameter (and not in the JSON body as written in the user guide). But the other parameters still have to be in the JSON body, which, of course, is weird.

Should I report a bug issue ?

frederikherema1
Star Contributor
Star Contributor
Yes, go ahead

othmann
Champ in-the-making
Champ in-the-making

lucassouza1
Champ in-the-making
Champ in-the-making
I had the same problem and it was solved adding a header content type to application/json

gumba
Champ in-the-making
Champ in-the-making
Hi Lucasousa1
I want to do the same but I keep getting error, see my code.
Could you give more tips on how did you managed to do this?
I have this code:
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;

public class RestAPI {

/**
  * @param args
  */
public static void main(String[] args) {
  try {
   // TODO Auto-generated method stub
   DefaultHttpClient client = new DefaultHttpClient();
        client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080),
            new UsernamePasswordCredentials("kermit", "kermit"));
                
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(new HttpHost("localhost", 8080, "http"), basicAuth);

        // Add AuthCache to the execution context
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
        HttpPost postMethod = new HttpPost("http://localhost:8080/activiti-rest/service/process-instance");
     
           postMethod.setEntity(new StringEntity("{\"processDefinitionKey\":\"MailSender\"}", "UTF-8"));
           HttpResponse response = client.execute(postMethod, localcontext);
           System.out.println(IOUtils.toString(response.getEntity().getContent()));
           client.getConnectionManager().shutdown();
        } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }
 
       
       
}

}

<html>
<head>
   <title>Status page</title>
</head>
<body style="font-family: sans-serif;">
<p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Internal Server Error</p>
<p>Failed to retrieve the process definition parameters</p>
<p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1">here</a>.<br>
Please continue your visit at our <a href="/">home page</a>.
</p>
</body>
</html>