skip to Main Content

I am trying to push a zipped folder contents to a azure repo using REST API call. I am using visual studio 2022 version with .NET6 C# code.
below is the code piece:

var fileContent = await new HttpClient().GetByteArrayAsync(folderZipUrl);
var fileBase64String = Convert.ToBase64String(fileContent);

//json = $"{{ "ref": "main", "path": ".", "message": "Initial commit", "content": "{fileBase64String}" }}";

var jsonObj = new
{
    refUpdates = "refs/heads/main",
    path = "./",
    message = "Initial commit",
    content = fileBase64String
};
string json = JsonSerializer.Serialize(jsonObj);

// push the new file
baseUrl = $"{baseUrl}/pushes?api-version=6.0";
response = await httpclient.PostAsync(baseUrl, new StringContent(json, Encoding.UTF8, "application/json"));

This code returns a response of Status code 400. Bad Request. I also tried declaring json content as per the commented line no 4.

I am creating an repository using this: https://learn.microsoft.com/en-us/rest/api/azure/devops/git/repositories/create?view=azure-devops-rest-7.0&tabs=HTTP

but the response is not the same as sample one given in the documentation. There are no fields for id or project in the actual response.

{
  "id": "76b510af-7910-4a96-9902-b978d6226bee",
  "name": "forkRepository",
  "url": "https://dev.azure.com/fabrikam/MyFirstProject/_apis/git/repositories/76b510af-7910-4a96-9902-b978d6226bee",
  "project": {
    "id": "3b046b6a-d070-4cd5-ad59-2eace5d05b90",
.
.
.
.
.

Could anyone tell me what is the problematic code here.

2

Answers


  1. Check the request here: Add a binary file

    {
      "refUpdates": [
        {
          "name": "refs/heads/master",
          "oldObjectId": "1380164a8118686087e38ce91f36b24b58c2df02"
        }
      ],
      "commits": [
        {
          "comment": "Added new image file.",
          "changes": [
            {
              "changeType": "add",
              "item": {
                "path": "/images/people/default.jpg"
              },
              "newContent": {
                "content": "/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8KCwkMEQ..........",
                "contentType": "base64encoded"
              }
            }
          ]
        }
      ]
    }
    

    And powershell example here: UpdateBinaryGitFile.ps1

    Login or Signup to reply.
  2. As a workaround to store shared build results, consider the following approaches:

    1. Publish your build artifacts as usual.. then download them in other pipelines through DownloadPipelineArtifact
    2. Publish your build results in universal artifacts. Check this link: Publish and download universal packages with Azure Artifacts
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search