Content
Create work package with custom fields through API
Added by Blaise D almost 9 years ago
Hello!
I am attempting to create work packages programmatically via the OpenProject API for a project with contains multiple custom fields. Unfortunately, I am running into a validation error with my work package payload. Following from the API documentation, I have posted to the ‘/work_packages/form’ endpoint to retrieve default values for all properties. After filling in my values, I still receive a validation error for a blank subject, even though it is filled out in my payload.
Here is my post using Python’s requests library:
response = requests.post(
'http://my_domain/api/v3/projects/11/work_packages/form',
auth=('apikey', API_KEY),
data=payload
)
Where an example payload would be the following:
{
"_links": {
"assignee": {
"href": "/api/v3/users/12",
"id": 12,
"name": "John Smith",
"title": "John Smith"
},
"category": {
"href": null
},
"customField3": {
"href": "/api/v3/string_objects?value=cf3_val",
"title": "cf3_val"
},
"customField4": {
"href": "/api/v3/string_objects?value=cf4_val",
"title": "cf4_val"
},
"parent": {
"href": null
},
"priority": {
"href": "/api/v3/priorities/8"
},
"responsible": {
"href": "/api/v3/users/12",
"id": 12,
"name": "John Smith",
"title": "John Smith"
},
"status": {
"href": "/api/v3/statuses/1"
},
"type": {
"href": "/api/v3/types/1"
},
"version": {
"href": null
}
},
"customField5": "123123",
"description": {
"format": "textile",
"html": "",
"raw": "Created from batch script"
},
"dueDate": "2016-11-10",
"estimatedTime": null,
"lockVersion": 0,
"parentId": null,
"percentageDone": 0,
"remainingTime": null,
"startDate": "2016-11-10",
"subject": "123123"
}
Here is the relevant schema information:
"schema": {
"_links": {},
"_type": "Schema",
<snip>
"subject": {
"maxLength": 255,
"minLength": 1,
"name": "Subject",
"required": true,
"type": "String",
"writable": true
},
And the validation errors returned:
"validationErrors": {
"subject": {
"_embedded": {
"details": {
"attribute": "subject"
}
},
"_type": "Error",
"errorIdentifier": "urn:openproject-org:api:v3:errors:PropertyConstraintViolation",
"message": "Subject can't be blank."
}
}
},
Any assistance would be greatly appreciated! I am at a loss as to what could be the problem, as the subject is included here in the same way that it is when creating a work package from the web interface.
Blaise
Replies (4)
Hi Blaise,
this message is typically returned when no payload has been transmitted with the POST request. I am aware that you seem to have sent a body with the request but OpenProject does not seem to think so.
When using your payload and posting it via e.g. Postman (after having adapted the links) I get an OK result.
Therefore I would look into the python code. I skimmed through the Request documentation and stumbled over this here:
http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests
where it notes:
As OpenProject expects a json encoded body, your code should pass a string and thus you should adapt your python code to the following:
response = requests.post( 'http://my_domain/api/v3/projects/11/work_packages/form', auth=('apikey', API_KEY), data=json.dumps(payload) )Hope that helps.
Regards
Jens
That did it!
I made the following changes to my Python script:
headers = { 'content-type': 'application/json' } response = requests.post( 'http://my_domain/api/v3/projects/11/work_packages/form', auth=('apikey', API_KEY), data=json.dumps(payload), headers=headers )and I now receive the 201 response and WorkPackage creation information confirming the package creation.
Thank you so much for your assistance, Jens! This was a subtle issue with my usage of the requests API that I was unable to recognize.
Cheers,
Blaise
Great, enjoy.
Hi,
I tried to create with this code a work packages:
var xhr2 = new XMLHttpRequest();
var data = ‘{subject subject“,”description“: {”format“: ”textile“, ”raw“: ”My raw
textileformatted description. Bye guys!“}, ”_links“: {”type“: {”href“:”/api/v3/types/1“, ”title“: ”Task“}, ”status“:{”href“:”/api/v3/statuses/1“}, ”priority“:{”href“:”/api/v3/priorities/8“, ”title“: ”normal“}, ”assignee“: {”href“: ”/api/v3/users/1"}}}}’;xhr2.open(‘GET’, ‘mydomain/openproject/api/v3/projects/dsds/work_packages/’);
xhr2.setRequestHeader(‘API_KEY’, ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’);
xhr2.setRequestHeader(‘Content-type’, ‘application/json’);
xhr2.onreadystatechange = function() {
if (xhr2.readyState 4 && xhr2.status 200) {
console.log(xhr2.responseText);
}
console.log(xhr2.readyState + “-” + xhr2.status);
};
xhr2.send(data);
Still i only get readyState == 4 and status = 0 in the console. the user admin is existing.
Do you know what the problem is? can someone help me?
Thank you for your help!
Best regards
Otmane