我正在使用 RestSharp v107。
我想更新測驗用例的迭代路徑。我可以使用 Postman 更新它,但使用 RestSharp 我收到“錯誤請求”-“您必須在請求正文中傳遞有效的補丁檔案。”
Azure Devops 更新作業項休息 API
我的代碼:
var client = new RestClient(@"https://dev.azure.com/krakhil/AkhilProject/_apis/wit/workitems/25?api-version=6.1-preview.3&Authorization=Basic BASE64PATSTRING");
client.Authenticator = new HttpBasicAuthenticator("", "My_PAT_I_HAVE_GENERATED");
var request = new RestRequest();
request.AddHeader("Content-Type", "application / json - patch json");
var body = @"[
" "\n"
@" {
" "\n"
@" ""op"": ""add"",
" "\n"
@" ""path"": ""/fields/System.IterationPath"",
" "\n"
@" ""value"": ""AkhilProject\\Sprint 1""
" "\n"
@" }
" "\n"
@"]";
request.AddParameter("application/json-patch json", body, ParameterType.RequestBody);
var response = client.PatchAsync(request).Result;
我已經嘗試過使用“AddJSONBody”、ExecuteAsync - 進行了相應的更改。仍然沒有幫助。
uj5u.com熱心網友回復:
有時閱讀檔案會有所幫助。
您需要做的就是:
var request = new RestRequest()
.AddStringBody(body, "application/json-patch json");
uj5u.com熱心網友回復:
RestSharp 作業代碼 - 根據Alexey Zimarev的輸入
var client = new RestClient(@"https://dev.azure.com/krakhil/AkhilProject/_apis/wit/workitems/25?api-version=6.1-preview.3");
client.Authenticator = new HttpBasicAuthenticator("", "YOUR PAT");
var request = new RestRequest();
request.AddHeader("Content-Type", "application/json-patch json");
var body = @"[
" "\n"
@" {
" "\n"
@" ""op"": ""add"",
" "\n"
@" ""path"": ""/fields/System.IterationPath"",
" "\n"
@" ""value"": ""AkhilProject\\Sprint 1""
" "\n"
@" }
" "\n"
@"]";
request.AddStringBody(body, DataFormat.Json);
var response = client.PatchAsync(request).Result;
使用HTTP 客戶端的作業代碼
string personalaccesstoken = "MY_PAT";
string organisation = "krakhil";
string project = "AkhilProject";
using HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "", personalaccesstoken))));
var obj = new[] { new { from = (string?)null, op = "add", path = "/fields/System.IterationPath", value = "AkhilProject\\Sprint 1" } };
string serialisedObj = System.Text.Json.JsonSerializer.Serialize(obj);
var content = new StringContent(
serialisedObj,
Encoding.UTF8,
"application/json-patch json");
HttpResponseMessage response = client.PatchAsync($"https://dev.azure.com/{organisation}/{project}/_apis/wit/workitems/25?api-version=6.1-preview.3",content).Result;
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsStringAsync().Result;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/455742.html
