我正在使用 Visual Studio Online REST API,除了创建工作项之外,似乎一切都可以正常工作。这不是本地安装。我按照这个示例发送了一个 PATCH,但收到了 400 Bad Request 错误。 VSO 创建工作项
每个提琴手,这是我的原始要求:
PATCH https://xxx.visualstudio.com/defaultcollection/myproject/_apis/wit/workitems/$Task?api-version=1.0 HTTP/1.1
Accept: application/json
Authorization: Basic RmI3etc_etc_etc==
Content-Type: application/json-patch+json; charset=utf-8
Host: xxx.visualstudio.com
Content-Length: 101
Expect: 100-continue
Connection: Keep-Alive
{"op":"add","path":"/fields/System.Title","value":"JavaScript implementation for Microsoft Account"}
我得到的响应是 400 Bad Request:
{"$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, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03ftoken0a3a","typeKey":"VssPropertyValidationException","errorCode":0,"eventId":3000}
我不确定为什么它说补丁文件无效。
更新:根据请求,仅共享更多代码。我修补了自己的图书馆。这是我为轻松将工作项添加到项目中所做的(产品积压、错误等)
public void AddWorkItem(VSOWorkItem workItem, string project)
{
Project = project;
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
//Set alternate credentials
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", UserName, Password))));
var workItems = new List<VSOFieldPatch>();
workItems.Add(new VSOFieldPatch() { Op = "add", Path = "/fields/" + VSOWorkItemFieldName.Title, Value = workItem.Title });
workItems.Add(new VSOFieldPatch() { Op = "add", Path = "/fields/" + VSOWorkItemFieldName.Description, Value = workItem.Description });
if (!string.IsNullOrEmpty(workItem.Tags))
workItems.Add(new VSOFieldPatch() { Op = "add", Path = "/fields/" + VSOWorkItemFieldName.Tags, Value = workItem.Tags });
var resp = AddWorkItemAsync(client, workItems, BaseProjectUrl + "wit/workitems/$" + workItem.WorkItemType.ToString());
}
}
private async Task<String> AddWorkItemAsync(HttpClient client,
IEnumerable<VSOFieldPatch> data,
String apiUrl)
{
var responseBody = String.Empty;
var temp = JsonConvert.SerializeObject(data);
var content = new StringContent(
JsonConvert.SerializeObject(data),
Encoding.UTF8,
"application/json-patch+json");
try
{
using (HttpResponseMessage response = client.PatchAsync(apiUrl + ApiVersion, content).Result)
{
response.EnsureSuccessStatusCode();
responseBody = await response.Content.ReadAsStringAsync();
}
}
catch (Exception ex)
{
}
return responseBody;
}
这是我的补丁扩展:
public static class HttpClientExtensions
{
public static Task<HttpResponseMessage> PatchAsync(this HttpClient client, string requestUri, HttpContent content)
{
HttpRequestMessage request = new HttpRequestMessage
{
Method = new HttpMethod("PATCH"),
RequestUri = new Uri(client.BaseAddress + requestUri),
Content = content,
};
return client.SendAsync(request);
}
}