我正在尝试使用 REST API 调用将压缩文件夹内容推送到 azure 存储库。我正在使用带有 .NET6 C# 代码的 visual studio 2022 版本。 下面是代码片段:
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"));
此代码返回状态代码 400 的响应。错误请求。我还尝试根据注释行 4 声明 json 内容。
谁能告诉我这里有问题的代码是什么。
作为存储共享构建结果的解决方法,请考虑以下方法:
在这里检查请求:添加二进制文件
{
"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"
}
}
]
}
]
}
此处为 powershell 示例:UpdateBinaryGitFile.ps1