使用Rest API从Azure Devops存储库获取文件

编程入门 行业动态 更新时间:2024-10-28 18:33:55
本文介绍了使用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( "dev.azure/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": "dev.azure/MS-ADM/xxxxxxxxxx/_apis/git/repositories/768454e2-4250-4d83-821b-7ac1c2707f5d/items?path=%2FTestResult&versionType=Branch&versionOptions=None", "_links": { "self": { "href": "dev.azure/MS-ADM/xxxxxxxx/_apis/git/repositories/768454e2-4250-4d83-821b-7ac1c2707f5d/items?path=%2FTestResult&versionType=Branch&versionOptions=None" }, "repository": { "href": "dev.azure/MS-ADM/xxxxxxxxx/_apis/git/repositories/768454e2-4250-4d83-821b-7ac1c2707f5d" }, "tree": { "href": "dev.azure/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 " dev.azure/MS-ADM/SAPBuild/_apis/git/repositories/SAPBuild/items?path=/TestResult&down​​load=true&api-version=5.0

The API you are using "dev.azure/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:

  • 如何使用Azure DevOps REST Api从Git存储库中下载分支中的文件?
  • 通过命令行从Azure Devops下载应用程序

更多推荐

使用Rest API从Azure Devops存储库获取文件

本文发布于:2023-10-07 10:20:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469120.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   API   Rest   Devops   Azure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!