[英]Update wiki page of tfs by calling rest-api
I want to update an already existing wiki page of tfs by using this document: https://docs.microsoft.com/de-de/rest/api/azure/devops/wiki/pages/create%20or%20update?view=azure-devops-rest-4.1我想使用此文档更新 tfs 的现有 wiki 页面: https ://docs.microsoft.com/de-de/rest/api/azure/devops/wiki/pages/create%20or%20update?view = azure-devops-rest-4.1
Creating a new wiki-page (with content) is no problem.创建一个新的 wiki 页面(带有内容)是没有问题的。 That is working fine.
那工作正常。 But I want to edit an existing one.
但我想编辑一个现有的。 The tfs-documentation says that the only difference in API call is to use an " If-Match "-header (see section Request Header ).
tfs-documentation 说 API 调用的唯一区别是使用“ If-Match ”-header(请参阅部分Request Header )。
Here I have 3 situations:我这里有3种情况:
I think the revision-hash (40 characters) should be a good way because the error message on non-40-chars returns the 400-error.我认为修订哈希(40 个字符)应该是一个好方法,因为非 40 字符上的错误消息返回 400 错误。
But it did not work?但它没有用? Has anyone an idea wich id tfs is wanting?
有没有人知道 id tfs 想要的想法? I used Postman and C# to update by api.
我使用 Postman 和 C# 通过 api 进行更新。 Below you can see my example code:
您可以在下面看到我的示例代码:
var handler = new HttpClientHandler()
{
UseDefaultCredentials = true,
UseProxy = false,
};
var client = new HttpClient(handler);
client.BaseAddress = new Uri(".../pages/pagename" + "?api-version=4.1");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var putContent = new StringContent("{ \"content\": \"New content for page\" }", Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.TryAddWithoutValidation("If-Match", "09f62be600a3b6d36d21b294dbb00921a5ba03ec");
var responseTask = client.PutAsync(client.BaseAddress, putContent);
var result = responseTask.Result;
var content = result.Content.ReadAsStringAsync().Result;
var code = result.StatusCode;
var body = content;
According to the Create or Update Wiki API , if we want to edit the wiki page, If-Match
header is required.根据Create or Update Wiki API ,如果我们要编辑 wiki 页面,则需要
If-Match
标头。 The value of If-Matchth
is the wiki page ETag. If-Matchth
的值是维基页面 ETag。
ETags can also be used for optimistic concurrency control, as a way to help prevent simultaneous updates of a resource from overwriting each other
ETag还可以用于乐观并发控制,作为一种帮助防止资源的同时更新相互覆盖的方法
so we need to get the wiki Etag before update.所以我们需要在更新前获取wiki Etag。 Please have a try to change the code as following:
请尝试更改代码如下:
var baseUrl = "xxxxx";
var handler = new HttpClientHandler()
{
UseDefaultCredentials = true,
UseProxy = false,
};
var client = new HttpClient(handler)
{
BaseAddress = new Uri(baseUrl)
};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "YourToken");
var getResult = client.GetAsync(baseUrl).Result;
var etag = getResult.Headers.GetValues("ETag");
var putContent = new StringContent("{ \"content\": \"New content for page\" }", Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.TryAddWithoutValidation("If-Match", etag);
var responseTask = client.PutAsync(client.BaseAddress, putContent);
var result = responseTask.Result;
var content = result.Content.ReadAsStringAsync().Result;
Test Result:测试结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.