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
Check the request here: Add a binary file
And powershell example here: UpdateBinaryGitFile.ps1
As a workaround to store shared build results, consider the following approaches: