3

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());
            }
        }

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"
        }
    }
}

What should I write in the url to get the file inside TestResult folder?

Sormita Chakraborty
  • 1,015
  • 2
  • 19
  • 36

1 Answers1

3

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

You should specify that particular file path instead of folder path such as path=path=%2FTestResult/**Home.cshtml**

Using a folder path, as you can see, the return json also shows "isFolder": true,.

More details and powershell sample, you could kindly refer below samples:

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62