Azure DevOps REST API-创建工作项-“需要一个值" [英] Azure DevOps REST API - Create Work Item - "A value is required"

查看:65
本文介绍了Azure DevOps REST API-创建工作项-“需要一个值"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Azure DevOps REST API创建工作项.我已经能够做其他事情,例如运行WIQL查询,但是当我尝试创建工作项时,出现了这个神秘的三重错误:

一个值是必需的,但在请求中不存在
值是必需的,但在请求中不存在
值是必需的,但在请求中不存在

这是完整的答复.

 <代码> {计数":1值":{消息":一个值是必需的,但在请求中不存在.\ r \ n一个值是必需的,但在请求中不存在.\ r \ n一个值是必需的,但在请求中不存在.r \ n}} 

这是我想要做的,遵循 const fetch = require('node-fetch');const username ='[用户名]';const password ='[个人访问令牌]'const organization ='[organization]';const project ='[project]'constauthorizationHeader =`Basic $ {Buffer.from(`$ {username}:$ {password}`).toString('base64')}`const body = [{"op":添加","path":"/fields/System.Title",值":"TestCreateWI";}];fetch(`https://dev.azure.com/$ {organization}/$ {project}/_apis/wit/workitems/$ Task?& api-version = 6.0`,{方法:"POST",标头:{授权:authorizationHeader,'内容类型':'应用程序/json-patch + json',},正文:JSON.stringify(body),}).then(async(response)=> {console.log(等待response.text())});

使用CURL的相同请求

  curl'https://dev.azure.com/MyOrganization/MyProject/_apis/wit/workitems/$Task?&api-version=6.0'\-H'授权:基本[已编辑]'\-H'内容类型:application/json-patch + json'\-数据二进制"[{{op":"add","path":"/fields/System.Title","value":"Test"}]' 

来自浏览器的相同请求

登录到DevOps,以便浏览器指向 https://dev.azure.com/YourProject/YourOrganization .然后打开开发工具(F5)并将此代码粘贴到JS控制台中.

 <代码>const body = [{"op":添加","path":"/fields/System.Title",值":"TestCreateWI";}];fetch(`$ {document.URL}/_ apis/wit/workitems/$ Task?& api-version = 6.0`,{方法:"POST",标头:{'内容类型':'应用程序/json-patch + json',},正文:JSON.stringify(body),}).then(async(response)=> {console.log(等待response.text())}); 

我知道它正在读取我的请求,因为如果我更改"op",到一个无效的值,我得到了另一个错误.我想念什么?

您的URL上有错字.我在邮递员中复制了该行为,并通过修复URL来解决了该问题.其他大多数回答都称呼工作".在PowerShell中没有复制您的错字.

您指定了 https://dev.azure.com/$ {organization}/$ {project}/_apis/wit/workitems/$Task?&api-version=6.0

它不应该有多余的&api版本之前 https://dev.azure.com/$ {organization}/$ {project}/_apis/wit/workitems/$Task?api-version=6.0

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?

解决方案

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

这篇关于Azure DevOps REST API-创建工作项-“需要一个值"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆