简体   繁体   English

通过调用 rest-api 更新 tfs 的 wiki 页面

[英]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种情况:

  • Using no "If-Match"-Header or an empty: Get a " 412 Precondition Failed " error.使用没有“如果-匹配” -Header或空:获得“412预处理失败”的错误。
  • Using a "If-Match"-Header with random value : Get a " 400 Bad Request " error.使用具有随机值的“If-Match”-Header:获得“ 400 Bad Request ”错误。
  • Using a "If-Match"-Header with exactly 40 characters (like the version-hash of the page-revision (eg '09f62be600a3b6d36d21b294dbb00921a5ba03ec')): Again " 412 Precondition Failed " error.使用恰好 40 个字符的“If-Match”-Header(如页面修订的版本哈希(例如'09f62be600a3b6d36d21b294dbb00921a5ba03ec')):再次出现“ 412 Precondition Failed ”错误。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM