cancel
Showing results for 
Search instead for 
Did you mean: 

REST api sample request to create task

rohitagrawal
Champ on-the-rise
Champ on-the-rise
as it's not available in documentation
16 REPLIES 16

frederikherema1
Star Contributor
Star Contributor
Thanks for reporting this. You can just do a POST to the runtime/tasks, with the body simmilar to the one used in the PUT runtime/tasks/1234 request.

I'll add the documentation for that to the current master.

rohitagrawal
Champ on-the-rise
Champ on-the-rise
http://localhost:8080/activiti-rest/service/runtime/tasks

getting error
{"errorMessage":"Method Not Allowed","statusCode":405}

please comment with sample request

frederikherema1
Star Contributor
Star Contributor
Sorry, POST that is Smiley Wink

skidvd
Champ in-the-making
Champ in-the-making
POST http://localhost:8080/activiti-rest/service/runtime/tasks/5

When I test with the above and providing data similar to the PUT update, I get a 403.  What else is required to create a task?

skidvd
Champ in-the-making
Champ in-the-making
Thank you for the link to the source code.  From reviewing the code in that area, I am still puzzled and wondering if it is even possible to create a task with the REST services as indicated in the earlier posts of this thread?

The 403 response code I was receiving was caused by spring's CSRF protection which is enabled by default.  Got around that.

Moving forward, in an attempt to create a new task, I have attempted calls similar to both of the following (as described above in this thread):

POST http://localhost:8080/activiti-rest/service/runtime/tasks/5
PUT http://localhost:8080/activiti-rest/service/runtime/tasks/5

Both of these calls have a payload similar to:

{
"assignee" : "kermit",
"delegationState" : "pending",
"description" : "new task description",
"dueDate" : "2013-04-17T13:06:02.438+02:00",
"name" : "new task name",
"owner" : "kermit",
"parentTaskId" : null,
"priority" : 20
}

The POST call is expecting an action in the payload, so this results in a 400 Bad Request response.  From looking at the org.activiti.rest.service.api.runtime.task.TaskResource source, this method only handles ACTION_COMPLETE, ACTION_CLAIM, ACTION_DELEGATE and ACTION_RESOLVE.  Therefore, I conclude that the POST variant is not correct for creating a new task.

The PUT variant holds a bit more promise.  Especially as the updateTasks() method mapped to this URI does call saveTask() which ultimately appears to handle creation of a new task in the invoked org.activiti.engine.impl.cmd.SaveTaskCmd.  Unfortunately, it does not get that far as it first calls org.activiti.rest.service.api.runtime.task.TaskBaseResource.getTaskFromRequest().  Line 455 of this method throws a new ActivitiObjectNotFoundException (resulting in a 404 Not Found response) if the task does not exist.

I cannot locate any other task related services that appear to handle task creation in either the documentation or the source.  I therefore believe that this is not supported (at least in the 5.17 version that I am using). 

Am I missing something simple?  If not, is this by design or a bug?

Incidentally, if I try the same operation via the Java API, I am able to create the task.  I can then subsequently retrieve it via the GET  http://localhost:8080/activiti-rest/service/runtime/tasks service.  This tells me that I am successfully authenticating and communicating with the REST services in general.  Just no luck with creating or updating a task via REST services.

I'd greatly appreciate any assistance you can provide please.

skidvd
Champ in-the-making
Champ in-the-making
I locally modified org.activiti.rest.service.api.runtime.task.TaskResouce at line 54 of the updateTask method.  I replaced:

Task task = getTaskFromRequest(taskId);

with

Task task = null;
    try {
     task = getTaskFromRequest(taskId);
    } catch (org.activiti.engine.ActivitiObjectNotFoundException ignore) {
     // We ignore this exception to enable creation of new tasks
     task = new org.activiti.engine.impl.persistence.entity.TaskEntity();
    }

This allows you to successfully create a new task via PUT to the associated URI.  However, this would require you to PUT with a URI containing an unkown taskId (let's say -1) to avoid updating an existing task and trigger the creation of a new one.  This is unfortunate at best. 

A more proper approach (better semantics)  would be to POST to a new request mapping …/runtime/tasks without any id so that it can be created dynamically.  Existing POST method requires a taskId and an action (no action to create).  I'll investigate this solution further and reply back in the near future. 

Would appreciate knowing if this is located somewhere else that I am missing, or if this is an intentional omission or a bug please?

skidvd
Champ in-the-making
Champ in-the-making
I added the following new method to org.activiti.rest.service.api.runtime.task.TaskResouce to enable new task creation via POST.  Id will be determined by Activiti.

  @RequestMapping(value="/runtime/task", method = RequestMethod.POST, produces="application/json")
  public TaskResponse createTask(
      @RequestBody TaskRequest taskRequest, HttpServletRequest request) {
   
    if (taskRequest == null) {
      throw new ActivitiException("A request body was expected when updating the task.");
    }
   
    Task task = new org.activiti.engine.impl.persistence.entity.TaskEntity();

    // Populate the task properties based on the request
    populateTaskFromRequest(task, taskRequest);
   
if (task.getId() != null) {
  throw new ActivitiIllegalArgumentException("Cannot create task with a specified id");
}

    // Save the task and fetch again, it's possible that an assignment-listener has updated
    // fields after it was saved so we can't use the in-memory task
    taskService.saveTask(task);
    task = taskService.createTaskQuery().taskId(task.getId()).singleResult();
   
    return restResponseFactory.createTaskResponse(task);
  }

Still would appreciate confirmation that this is not located elsewhere and I have somehow missed it in my searches.  Also would like to know if this is an intentional omission from the REST services?

trademak
Star Contributor
Star Contributor
It's indeed missing from the userguide, could you create a JIRA issue for that?
The REST service does exists however: POST /runtime/tasks

Best regards,