我正在努力推动对VSTS Git存储库中现有文件的更新。 文档https://docs.microsoft.com/en-us/rest/api/vsts/git/pushes/create#update_a_file显示了该做什么。
当我使用GET
代替POST
时,我会获得所有现有推送。所以基本上请求正在运行。这是我的网址https://mine.visualstudio.com/_apis/git/repositories/ad3d7615-6e4a-4988-bd3f-ca80d7ec9791/pushes?api-version=4.1-preview。
我正在测试来自控制台应用程序的请求。正文是从稍后序列化的动态创建的。 filecontent是一个字符串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:'响应状态代码没有 表示成功:404(未找到)。
正文是有效的JSON。
某些API调用需要网址 .vsrm.visualstudio.com 而不是 .visualstudio.com 。但不是在这种情况下。
我没有想法。是否有人成功将更新推送到VSTS,并可以告诉我该怎么做?
答案 0 :(得分:0)
正如评论中所提到的,我能够add
代替edit
。
解决方案是首先签入文件,然后发送更新/编辑。
如果没有现有文件,编辑将失败并显示异常。