napsebefya
napsebefya

Reputation: 353

Why does Azure Devops create work-item return 404 page not found?

I'm using the Azure DevOps API to create a new work item. However I get an error

404 page not found

Documentation: Work Items - Create

Things I've tried

  1. Using postman and powershell (see screenshot and script below). enter image description here
  2. In the json data I am populating only the required fields. The various values come from get API calls (e.g. get iterations).
  3. Changed the API version (6.0, 5.1)
  4. Included / excluded the project name in the URL
  5. Created a new personal access token

Get calls work (e.g. get list of teams, projects, work item types)

Why the 404 error for post?

Below the powershell script

$url= "https://dev.azure.com/my-org/my-project/_apis/wit/workitems/$($witType)?api-version=6.0"
$JSON = @'
  [
  {
    "op": "add",
    "path": "/fields/System.Title",
    "from": null,
    "value": "Sample task"
  }    ,
{
  "op": "add",
  "path": "/fields/System.IterationId",
  "from": null,
  "value": "e5c8d590-5283-4642-8262-716d083bc045"      
},
{
  "op": "add",
  "path": "/fields/System.AreaId",
  "from": null,
  "value": "138233"      
},
{
  "op": "add",
  "path": "/fields/System.State",
  "from": null,
  "value": "To Do"      
}
]
'@


Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json-patch+json

Upvotes: 2

Views: 1778

Answers (1)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35194

The issue should be related to the Rest API URL.

When you run the Rest API in Postman, you need to add $ before the work item type.

For example:

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

When you use the PowerShell to run the Rest API, you need to modify the URL.

https://dev.azure.com/my-org/my-project/_apis/wit/workitems/`$$($witType)?api-version=6.0

Here is an example:

$url= "https://dev.azure.com/my-org/my-project/_apis/wit/workitems/`$$($witType)?api-version=6.0"
$JSON = @'
  [
  {
    "op": "add",
    "path": "/fields/System.Title",
    "from": null,
    "value": "Sample task"
  }    ,
{
  "op": "add",
  "path": "/fields/System.IterationId",
  "from": null,
  "value": "e5c8d590-5283-4642-8262-716d083bc045"      
},
{
  "op": "add",
  "path": "/fields/System.AreaId",
  "from": null,
  "value": "138233"      
},
{
  "op": "add",
  "path": "/fields/System.State",
  "from": null,
  "value": "To Do"      
}
]
'@


Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json-patch+json

Refer to this ticket: Azure Devops bulk create Tasks for every Product Backlog Item

Upvotes: 5

Related Questions