Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 271
Patrick McElhaney Avatar asked Nov 10 '20 14:11

Patrick McElhaney


2 Answers

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

like image 147
Matt Avatar answered Sep 28 '22 19:09

Matt


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}

like image 23
Shamrai Aleksander Avatar answered Sep 28 '22 20:09

Shamrai Aleksander