Patrick McElhaney
Patrick McElhaney

Reputation: 59271

Azure DevOps REST API - Create Work Item - "A value is required"

I'm trying to create a work item using the Azure DevOps REST API. I've been able to do other things, like running a WIQL query, but when I try to create a work item I get this mysterious triple-error:

A value is required but was not present in the request
A value is required but was not present in the request
A value is required but was not present in the request

Here's the full response.

{
    "count": 1,
    "value": {
        "Message": "A value is required but was not present in the request.\r\nA value is required but was not present in the request.\r\nA value is required but was not present in the request.\r\n"
    }
}

Here's what I'm trying to do, following the documentation as best I can.

Note: as the accepted answer called out, the problem was a typo, an & immediately following the ? in the URL. Since these examples otherwise work, for the benefit of anyone who wants to copy and paste, I've fixed the typo.

Minimal test case in NodeJS

const fetch = require('node-fetch');

const username = '[username]';
const password = '[personal access token]'
const organization = '[organization]';
const project = '[project]'

const authorizationHeader = `Basic ${Buffer.from(
    `${username}:${password}`
  ).toString('base64')}`

const body = [
    { 
        "op":"add",
        "path":"/fields/System.Title",
        "value":"TestCreateWI"
    }
];


  fetch(`https://dev.azure.com/${organization}/${project}/_apis/wit/workitems/$Task?api-version=6.0`, {
    method: 'POST',
    headers: {
      Authorization: authorizationHeader,
      'Content-Type': 'application/json-patch+json',
    },
    body: JSON.stringify(body),
  }).then(async (response) => {    
    console.log(await response.text())
  });

Same request using CURL

curl 'https://dev.azure.com/MyOrganization/MyProject/_apis/wit/workitems/$Task?api-version=6.0' \
  -H 'Authorization: Basic [redacted]' \
  -H 'Content-Type: application/json-patch+json' \
  --data-binary '[{"op":"add","path":"/fields/System.Title","value":"Test"}]'

Same request from a browser

Log in to DevOps so that your browser is pointing to https://dev.azure.com/YourProject/YourOrganization. Then open Dev Tools (F5) and paste this code into the JS console.


const body = [
    { 
        "op":"add",
        "path":"/fields/System.Title",
        "value":"TestCreateWI"
    }
];

fetch(`${document.URL}/_apis/wit/workitems/$Task?api-version=6.0`, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json-patch+json',
  },
  body: JSON.stringify(body),
}).then(async (response) => {    
  console.log(await response.text())
});

I know that it's reading my request, because if I change "op" to an invalid value, I get a different error. What am I missing?

Upvotes: 9

Views: 5577

Answers (4)

Matt
Matt

Reputation: 4045

You have a typo on your URL. I duplicated the behavior in postman and resolved it by fixing the URL. Most of the other answers with the calls "working" in PowerShell didn't copy your typo.

You specified https://dev.azure.com/${organization}/${project}/_apis/wit/workitems/$Task?&api-version=6.0

It shouldn't have the extra & before the api-version https://dev.azure.com/${organization}/${project}/_apis/wit/workitems/$Task?api-version=6.0

Upvotes: 10

Jane Ma-MSFT
Jane Ma-MSFT

Reputation: 5222

You can try substituting ' for " on both sides of the URL in your script.

I tried the PowerShell script provided in the link you provided and successfully reproduced the error.

enter image description here

After I changed the ' on both sides of $uri to ", I was able to successfully create the work item.

In addition, during my tests, I used %24 instead of $ in the URI, otherwise an error would be reported.

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

PS. My test environment is Windows PowerShell.

Upvotes: -1

Leo Liu
Leo Liu

Reputation: 76750

Azure DevOps REST API - Create Work Item - “A value is required”

I could use the REST API Work Items - Create to create the workitem with Powershell task in my pipeline:

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

The request body is:

  [
    {
      "op": "add",
      "path": "/fields/System.Title",
      "value": "TestCreateWI"
    }
 ]

Below is my powershell scripts:

$connectionToken="$(PAT)"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$url= 'https://dev.azure.com/MyCustomOrganization/MyTestProject/_apis/wit/workitems/$task?api-version=6.0'

$body=@"
  [
    {
      "op": "add",
      "path": "/fields/System.Title",
      "value": "TestCreateWI"
    }
 ]
"@

Write-Host "$url"
$response= Invoke-RestMethod -Uri $url  -ContentType "application/json-patch+json" -Body $body -headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST

The test result:

enter image description here

enter image description here

Upvotes: -1

Shamrai Aleksander
Shamrai Aleksander

Reputation: 16018

I do not use curl on my tasks, but the following works on my org:

curl -u "":personal_access_token -d "[{\"op\":\"add\",\"path\":\"/fields/System.Title\",\"value\":\"Sample task\"}]" -H "Content-Type: application/json-patch+json" -X POST https://dev.azure.com/<org>/<project>/_apis/wit/workitems/${Task}?api-version=6.0 

I`ve tested curl for windows 7.73.0.

Docs to create a personal access token: Use personal access tokens. Additionally, use the work item type in url like ${work item type name}

If I post data with -d '[{"op":"add","path":"/fields/System.Title","value":"Sample task"}]' the service returns the following answer:

{"$id":"1","innerException":null,"message":"You must pass a valid patch document in the body of the request.","typeName":"Microsoft.VisualStudio.Services.Common.VssPropertyValidationException, Microsoft.VisualStudio.Services.Common","typeKey":"VssPropertyValidationException","errorCode":0,"eventId":3000}

Upvotes: 1

Related Questions