通过 REST 将更新推送到 VSTS API
Pushing an update to VSTS via REST API
我正在努力将更新推送到 VSTS Git 存储库中的现有文件。
文档 https://docs.microsoft.com/en-us/rest/api/vsts/git/pushes/create#update_a_file 显示了要做什么。
当我使用 GET
而不是 POST
时,我得到了所有现有的推送。所以基本上请求是有效的。这是我的 URL https://mine.visualstudio.com/_apis/git/repositories/ad3d7615-6e4a-4988-bd3f-ca80d7ec9791/pushes?api-version=4.1-preview.
我正在测试来自控制台应用程序的请求。正文是从稍后序列化的动态创建的。文件内容是一个字符串 var x=1;
.
dynamic body = new
{
refUpdates = new[] { new { name = "refs/heads/master", oldObjectId = branchObjectId } },
commits = new[] {
new {
comment = "Updated Configuration" ,
changes = new[] {
new {
changeType = "edit",
item = new { path = "/files/filename.js"},
newContent = new { content = filecontent, contentType = "rawtext" }
}
}
}
}
};
请求如下所示:
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
byte[] login = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalaccesstoken));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(login));
var content = new StringContent(body, Encoding.UTF8, "application/json");
using (HttpResponseMessage response = client.PostAsync(url, content).Result)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
responseBodyAction.Invoke(responseBody);
}
}
没有成功,抛出以下异常:
System.Net.Http.HttpRequestException: 'Response status code does not
indicate success: 404 (Not Found).
正文是有效的 JSON。
有些 API 调用需要 url .vsrm.visualstudio.com 而不是 .visualstudio.com。但在这种情况下不是。
我没主意了。有人成功推送了对 VSTS 的更新并且可以告诉我怎么做吗?
如评论中所述,我能够 add
而不是 edit
。
解决方案是先签入一个文件,然后然后发送一个update/edit。
如果不存在现有文件,编辑将失败并出现异常。
我正在努力将更新推送到 VSTS Git 存储库中的现有文件。 文档 https://docs.microsoft.com/en-us/rest/api/vsts/git/pushes/create#update_a_file 显示了要做什么。
当我使用 GET
而不是 POST
时,我得到了所有现有的推送。所以基本上请求是有效的。这是我的 URL https://mine.visualstudio.com/_apis/git/repositories/ad3d7615-6e4a-4988-bd3f-ca80d7ec9791/pushes?api-version=4.1-preview.
我正在测试来自控制台应用程序的请求。正文是从稍后序列化的动态创建的。文件内容是一个字符串 var x=1;
.
dynamic body = new
{
refUpdates = new[] { new { name = "refs/heads/master", oldObjectId = branchObjectId } },
commits = new[] {
new {
comment = "Updated Configuration" ,
changes = new[] {
new {
changeType = "edit",
item = new { path = "/files/filename.js"},
newContent = new { content = filecontent, contentType = "rawtext" }
}
}
}
}
};
请求如下所示:
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
byte[] login = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalaccesstoken));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(login));
var content = new StringContent(body, Encoding.UTF8, "application/json");
using (HttpResponseMessage response = client.PostAsync(url, content).Result)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
responseBodyAction.Invoke(responseBody);
}
}
没有成功,抛出以下异常:
System.Net.Http.HttpRequestException: 'Response status code does not indicate success: 404 (Not Found).
正文是有效的 JSON。
有些 API 调用需要 url .vsrm.visualstudio.com 而不是 .visualstudio.com。但在这种情况下不是。
我没主意了。有人成功推送了对 VSTS 的更新并且可以告诉我怎么做吗?
如评论中所述,我能够 add
而不是 edit
。
解决方案是先签入一个文件,然后然后发送一个update/edit。
如果不存在现有文件,编辑将失败并出现异常。