Search code examples
jsonapipostgetpivotaltracker

Pivotal Tracker API Labels


I am trying to use the Pivotal Tracker API to post a story using python. I am able to do so using the python requests module. The following is a sample code that I can use to create a new story:

payload = {"name":"Create story w/create label"}
requests.post('https://www.pivotaltracker.com/services/v5/projects/xxxxxx/stories', data=payload4, headers={'X-TrackerToken':token}).json()

for which the output is

{u'created_at': u'2015-03-04T18:47:28Z',
 u'current_state': u'unscheduled',
 u'id': xxxxxx,
 u'kind': u'story',
 u'labels': [],
 u'name': u'Create story w/create label',
 u'owner_ids': [],
 u'project_id': xxxxxx,
 u'requested_by_id': xxxxxx,
 u'story_type': u'feature',
 u'updated_at': u'2015-03-04T18:47:28Z',
 u'url': u'https://www.pivotaltracker.com/story/show/xxxxxx'}

Great. Now, I want to create a story and add a label to it. According to the POST /projects/{project_id}/stories API on https://www.pivotaltracker.com/help/api/rest/v5, I should be able to format my json as follows and run a POST request:

payload = {"name":"Create story w/create label","labels":[{"name":"orbit"}]}
requests.post('https://www.pivotaltracker.com/services/v5/projects/xxxxxx/stories', data=payload, headers={'X-TrackerToken':token}).json()

however, I get the following 400 response:

{u'code': u'invalid_parameter',
 u'error': u'One or more request parameters was missing or invalid.',
 u'general_problem': u"'labels' must be an array of label values",
 u'kind': u'error'}

From what I understand, the way I formatted the payload json is correct and the label resource json is formatted properly. I'm not sure if the error is on my end or if it is something else. If someone with knowledge of the API could provide some help, it would be much appreciated.

Thanks


Solution

  • Solved it, there' s a JSON encoding issue. We never told pivotal tracker that we were sending JSON. This code snippet works: data = { "labels": ["major request"], "name": "some cool feature", "description": "solve world hunger", "comments": ["requested by not the 1%"] } headers = {'X-TrackerToken': TRACKER_TOKEN, 'Content-type': 'application/json', 'Accept': 'application/json' } return requests.post(url, headers=headers, data=json.dumps(data)) Need to tell the API that we are sending JSON and accepting JSON.