Home > Net >  REST API is not downloading file from AZURE git branch
REST API is not downloading file from AZURE git branch

Time:01-12

I am new to C# and trying to download a file from azure git repo. The response status is success but not downloading the file to local drive. I m passing all parameters correctly.

 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/{organization}/{project}/_apis/git/repositories/{repositoryId}/items?path={path}&download={download}&versionDescriptor.versionType={versionDescriptor_versionType}&versionDescriptor.version={versionDescriptor_version}&api-version=7.0").Result)
        {
            response.EnsureSuccessStatusCode();
             var responseBody = await response.Content.ReadAsStringAsync();

            Console.WriteLine(responseBody);

        }
}

and the output i am getting as below.

{
  "objectId": "34e9cd84219e51533b19b3a70121e73b612c0eca",
  "gitObjectType": "blob",
  "commitId": "1916fd237ce345505a0cdbb285ce0ba71a64691b",
  "path": "/Backend/DATA/Document_AA083_A100001.xml",
  "url": "https://dev.azure.com/xxxx-xxx-xxx/00e7c28c-efde-450e-91a1-133b7f2a228c/_apis/git/repositories/062ef079-ab27-4ed9-ba91-aaa0678d89b5/items?path=/Backend/DATA/Document_AA083_A100001.xml&versionType=Branch&version=tcs00107/GIT_Tool&versionOptions=None",
  "_links": {
    "self": {
      "href": "https://dev.azure.com/xxxx-xxx-xxx/00e7c28c-efde-450e-91a1-133b7f2a228c/_apis/git/repositories/062ef079-ab27-4ed9-ba91-aaa0678d89b5/items?path=/Backend/DATA/Document_AA083_A100001.xml&versionType=Branch&version=tcs00107/GIT_Tool&versionOptions=None"
    },
    "repository": {
      "href": "https://dev.azure.com/xxxx-xxx-xxx/00e7c28c-efde-450e-91a1-133b7f2a228c/_apis/git/repositories/062ef079-ab27-4ed9-ba91-aaa0678d89b5"
    },
    "blob": {
      "href": "https://dev.azure.com/xxxx-xxx-xxx/00e7c28c-efde-450e-91a1-133b7f2a228c/_apis/git/repositories/062ef079-ab27-4ed9-ba91-aaa0678d89b5/blobs/34e9cd84219e51533b19b3a70121e73b612c0eca"
    }
  }
}

It would be really helpful if anyone Could help me to resolve this issue . Much appreciated for your help.

CodePudding user response:

The problem is the JSON response you are getting, consists of metadata about the file. You have to use the "url" and perform another get request using "HttpClient.GetByteArrayAsync()". This gets the file as a byte array, which can then be written to your local drive using "File.WriteAllBytes(string, byte[])".

using (HttpClient client = new HttpClient())
{
    //.. your existing code 
    var responseBody = await response.Content.ReadAsStringAsync();
    dynamic jsonData = JsonConvert.DeserializeObject(responseBody);
    var blobUrl = jsonData._links.blob.href;
    byte[] fileBytes = await client.GetByteArrayAsync(blobUrl);
    File.WriteAllBytes("path/to/local/file", fileBytes);
}

Change the path to where you want the file saved, and be aware of any permissions/restrictions to the filepath.

CodePudding user response:

Maybe try reinstalling it, and if not, try googling it?

I'm honestly not too knowledgeable about the subject

-N1CKN0TAD3

  • Related