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

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-05 10:23:47

问题


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.

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?


回答1:


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




回答2:


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}




回答3:


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:




回答4:


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.

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.



来源:https://stackoverflow.com/questions/64771092/azure-devops-rest-api-create-work-item-a-value-is-required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!