使用Rest API从Azure Devops存储库获取文件
我正在尝试使用以下代码从AzureDevops存储库中获取特定文件:
I am trying to get a particular file from AzureDevops repository using the following code:
public static async void GetFile()
{
try
{
var personalaccesstoken = "XXXXXXXXX";
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))));
using (HttpResponseMessage response = client.GetAsync(
"https://dev.azure.com/MS-ADM/SAPBuild/_apis/git/repositories/SAPBuild/items?path=/TestResult&download=true&api-version=5.0").Result)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
Console.ReadLine();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
但是我返回了以下json:
But I am getting back the following json back:
{
"objectId": "08205d1ecfa86e4d8a2451fa189e68711398f126",
"gitObjectType": "tree",
"commitId": "xxxxxxxxxxxxxxxx",
"path": "/TestResult",
"isFolder": true,
"url": "https://dev.azure.com/MS-ADM/xxxxxxxxxx/_apis/git/repositories/768454e2-4250-4d83-821b-7ac1c2707f5d/items?path=%2FTestResult&versionType=Branch&versionOptions=None",
"_links": {
"self": {
"href": "https://dev.azure.com/MS-ADM/xxxxxxxx/_apis/git/repositories/768454e2-4250-4d83-821b-7ac1c2707f5d/items?path=%2FTestResult&versionType=Branch&versionOptions=None"
},
"repository": {
"href": "https://dev.azure.com/MS-ADM/xxxxxxxxx/_apis/git/repositories/768454e2-4250-4d83-821b-7ac1c2707f5d"
},
"tree": {
"href": "https://dev.azure.com/MS-ADM/xxxxxxx/_apis/git/repositories/768454e2-4250-4d83-821b-7ac1c2707f5d/trees/08205d1ecfa86e4d8a2451fa189e68711398f126"
}
}
}
要在TestResult文件夹中获取文件,我应该在URL中写些什么?
What should I write in the url to get the file inside TestResult folder?
您正在使用的API " https://dev.azure.com/MS-ADM/SAPBuild/_apis/git/repositories/SAPBuild/items?path=/TestResult&download=true&api-version=5.0
The API you are using "https://dev.azure.com/MS-ADM/SAPBuild/_apis/git/repositories/SAPBuild/items?path=/TestResult&download=true&api-version=5.0
您应该指定特定的文件路径,而不是文件夹路径,例如 path = path =%2FTestResult/** Home.cshtml **
You should specify that particular file path instead of folder path such as path=path=%2FTestResult/**Home.cshtml**
如您所见,使用文件夹路径,返回json还显示"isFolder":true,
.
Using a folder path, as you can see, the return json also shows "isFolder": true,
.
更多详细信息和powershell示例,请参考以下示例:
More details and powershell sample, you could kindly refer below samples: