Create workitem in Azure Devops from Postman

We are looking to move from Jira to Azure Devops with all of our projects, one of those being our helpdesk functionality.

I have been tasked with porting our current solution regarding tickets: We have a Laravel project running that checks certain email addresses for new help requests. When a new email arrives it sends a notification to our Slack where one of the team members acknowledge it and chooses where to create a ticket in Jira. Then it creates a new ticket in Jira with an api-call, based on the choice in Slack.

I have found and tried out the Azure Devops API, and can list projects and get workitems and so on. But I cannot POST a new workitem by calling

http://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$task?api-version=6.1-preview.3&bypassRules=true

with a JSON body like this:

[
    {
        "op": "add",
        "path": "/fields/System.Title",
        "from": null,
        "value": "Test of REST functionality"
    },
    {
        "op": "add",
        "path": "/fields/System.State",
        "from": null,
        "value": "New"
    }
]

I get a JSON response like this

{
    "fields": {
        "System.WorkItemType": "Task",
        "System.AreaPath": "xxxxx",
        "System.TeamProject": "xxxx",
        "System.IterationPath": "xxxx",
        "System.State": "New",
        "System.Reason": "New",
        "Microsoft.VSTS.Common.StateChangeDate": "1753-01-01T00:00:00Z",
        "System.ChangedBy": {
            "displayName": "xxxx",
            "url": "xxxx",
            "_links": {
                "avatar": {
                    "href": "xxxx"
                }
            },
            "id": "xxxx",
            "uniqueName": "xxxx",
            "imageUrl": "xxxx",
            "descriptor": "xxxx"
        },
        "System.CreatedBy": {
            "displayName": "xxxx",
            "url": "xxxx",
            "_links": {
                "avatar": {
                    "href": "xxxx"
                }
            },
            "id": "xxxx",
            "uniqueName": "xxxx",
            "imageUrl": "xxxx",
            "descriptor": "xxxx"
        },
        "Microsoft.VSTS.Common.Priority": 2
    },
    "_links": {
        "workItemType": {
            "href": "https://dev.azure.com/{organization}/{project}/_apis/wit/workItemTypes/Task"
        },
        "fields": {
            "href": "https://dev.azure.com/{organization}/{project}/_apis/wit/fields"
        }
    },
    "url": "https://dev.azure.com/{organization}/{project}/_apis/wit/workItems"
}

That last url refers me to a page with a JSON object containing this message:

No HTTP resource was found that matches the request URI 'https://dev.azure.com/{project}/_apis/wit/workItems'

and no new tasks are created.

I started with consulting the documentation.

I have tried editing and regenerating my Personal Access Token with several different combinations of permissions regarding work items. I have tried giving the PAT full access.

I have tried calling different versions of the API.

I have tried adding fields besides title and state that are listed as required, ending with setting bypassRules to true.

Google and the /r/azuredevops subreddit returned no results that worked.

Can anyone point me in the direction of where I should go, or what I'm missing? Thanks in advance :-)

EDIT: I am POSTing.

POST

EDIT FOR SOLUTION:

I feel so stupid. When POSTing in Postman, I used http:// instead of https:// Using https:// solved the issue.

Upvotes: 2

Views: 6943

Answers (2)

Vito Liu
Vito Liu

Reputation: 8298

Please check the pic, according to the request body, it seems that your are using the REST API get work item type definition instead of Create Work Item

If the method is GET, it will get the work item type.

Request Body:

GET https://dev.azure.com/{organization}/{project}/_apis/wit/workitemtypes/{type}?api-version=6.0

the JSON response like this:

    {
            "fields": {
                  .....
                },
            "_links": {
                  .....                    
                },
            "url": "https://dev.azure.com/{organization}/{project}/_apis/wit/workItems"
        }

Postman Result:

enter image description here

If we need create work item, we need change the method to POST

Request URL:

POST https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${type}?api-version=6.0

Request Body:

[
    {
        "op": "add",
        "path": "/fields/System.Title",
        "from": null,
        "value": "Test of REST functionality"
    },
    {
        "op": "add",
        "path": "/fields/System.State",
        "from": null,
        "value": "New"
    }
]

the JSON response like this:

{
    "id": {Work Item ID},
    "rev": 1,
    "fields": {
      ...
    },
    "_links": {
      ...
    },
    "url": "https://dev.azure.com/{organization}/{project}/_apis/wit/workItems/{Work Item ID}"
}

Postman Result:

enter image description here

And Azure DevOps Service result:

enter image description here

Update1

but I'd like to know what you have in your url

My URL is

https://dev.azure.com/{Org name}/{Project name}/_apis/wit/workitems/$task?api-version=6.0

You have 12 headers and I have 9, and my url seems longer than yours

  1. Create a new request page in the postman, the default Header is 7, enter Request URL, it will add the header Cookie and now the Header is 8.

enter image description here

  1. Change the method from GET to POST, it will add the header Content-Length and now the Header is 9.

enter image description here

  1. Click the Body tab->select raw and change the content type to JSON to add Request Body, it will add the header Content-Type and now the Header is 10

enter image description here

  1. Click the Authorization tab->select Basic Auth and enter the PAT Token as password, it will add the header Authorization and now the Header is 11.

enter image description here

  1. Now, when we click the Send button, we will get the error message The request indicated a Content-Type of \"application/json\" for method type \"POST\" which is not supported. Valid content types for this method are: application/json-patch+json.

enter image description here

  1. Click the Header tab, the default value of Content-Type is application/json, we need to disable it and add new header Content-Type and set the value to application/json-patch+json, and now the Header is 12

enter image description here

  1. Click the button Send and response code is 200

enter image description here

Upvotes: 4

Mohamed Sahbi
Mohamed Sahbi

Reputation: 1143

Your are attempting to create more than one task, which is not supported by this endpoint according to the documentation
enter image description here.

I managed to create a task with the following body:

[
  {
    "op": "add",
    "path": "/fields/System.Title",
    "from": null,
    "value": "Sample task"
  }
]

Headers included:

  • Content-Type : application/json-patch+json
  • Authentication : Basic encoded-PAT

The content type header is mentioned in the Body Request section in the documentation.

For the PAT Token, you only need vso.work_write, also mentioned in the documentation.

The endpoint Url is: https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$Task?api-version=6.0

The response body looks like this

{
    "id": 8921,
    "rev": 1,
    "fields": {
        "System.AreaPath": "Project",
        "System.TeamProject": "Project",
        "System.IterationPath": "Project",
        "System.WorkItemType": "Task",
        "System.State": "New",
        "System.Reason": "New",
        "System.CreatedDate": "2021-03-17T10:36:25.513Z",
        "System.CreatedBy": ....,
        "System.ChangedDate": "2021-03-17T10:36:25.513Z",
        "System.ChangedBy": ....,
        "System.CommentCount": 0,
        "System.Title": "Sample task",
        "Microsoft.VSTS.Common.StateChangeDate": "2021-03-17T10:36:25.513Z",
        "Microsoft.VSTS.Common.Priority": 2
    },
    "_links": { some links
    },
    "url": "https://dev.azure.com/Organization/14360bf9-7578-4sfs-b43f-8dfda139fb17/_apis/wit/workItems/8921"
}

The final attribute url includes the correct link to the newly created WorkItem.

Upvotes: 0

Related Questions