AzureDevops Api: Get item API with download true return a json

Viewed 56

I'm trying to download a Git File using C#. I use the following code:

Stream response = await client.GetStreamAsync(url);
var splitpath = path.Split("/");
Stream file = File.OpenWrite(splitpath[splitpath.Length - 1]);
response.CopyToAsync(file);
response.Close();
file.Close();

Following this documentation, I use the following url:

string url = mainurl + name + "/_apis/git/repositories/" + rep + "/items?path=" + path + "&download=true&api-version=6.0";

but the file saved contains a json containing different links and information about the git file.

To check if all was working well, I tried to download it in a zip format, using the following url:

string url = mainurl + name + "/_apis/git/repositories/" + rep + "/items?path=" + path + "&$format=zip";

And it works fine, the file downloaded is a zip file containing the original file with its content...

Can someone help me? Thanks

P.S. I know that I can set IncludeContent to True, and get the content in the json, but I need the original file.

1 Answers

Since you are using C#, I will give you a C# sample to get the original files:

using RestSharp;
using System;
using System.IO;
using System.IO.Compression;

namespace xxx
{
    class Program
    {
        static void Main(string[] args)
        {
            string OrganizationName = "xxx";
            string ProjectName = "xxx";
            string RepositoryName = "xxx";
            string Personal_Access_Token = "xxx";

            string archive_path = "./"+RepositoryName+".zip";
            string extract_path = "./"+RepositoryName+"";

            string url = "https://dev.azure.com/"+OrganizationName+"/"+ProjectName+"/_apis/git/repositories/"+RepositoryName+"/items?$format=zip&api-version=6.0";
            var client = new RestClient(url);
            //client.Timeout = -1;
            var request = new RestRequest(url, Method.Get);
            request.AddHeader("Authorization", "Basic "+Personal_Access_Token);
            var response = client.Execute(request);
            //save the zip file
            File.WriteAllBytes("./PushBack.zip", response.RawBytes);

            //unzip the file
            if (Directory.Exists(extract_path))
            {
                Directory.Delete(extract_path, true);
                ZipFile.ExtractToDirectory(archive_path, extract_path);
            }
            else
            {
                ZipFile.ExtractToDirectory(archive_path, extract_path);
            }
        }
    }
}

Successfully on my side:

enter image description here

Let me know whether this works on your side.

Related