You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过REST API更新VSTS Git文件失败求助(404错误)

Troubleshooting 404 Error When Updating VSTS Git File via REST API

Let’s break down the likely causes of your 404 Not Found error and walk through fixes step by step:

1. Critical Fix: Missing JSON Serialization for Request Body

The biggest issue in your code is how you’re constructing the request body—you’re passing the anonymous dynamic object directly to StringContent, which doesn’t automatically serialize it to valid JSON. This means your API endpoint is receiving a raw object string (like { refUpdates = System.Object[]... }) instead of properly formatted JSON, which will cause unexpected errors like 404.

Fix this by serializing the object first. If you’re using Newtonsoft.Json (Json.NET):

using Newtonsoft.Json;

// ...

var jsonBody = JsonConvert.SerializeObject(body);
var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");

Or use the built-in System.Text.Json if you prefer:

using System.Text.Json;

// ...

var jsonBody = JsonSerializer.Serialize(body);
var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");

2. Update to a Supported API Version

Your request uses api-version=4.1-preview, which is an outdated preview version. Azure DevOps regularly retires old API endpoints, so using an unsupported version can lead to 404s. Switch to a recent stable version like:

https://mine.visualstudio.com/_apis/git/repositories/ad3d7615-6e4a-4988-bd3f-ca80d7ec9791/pushes?api-version=7.1

3. Verify the URL Structure

If your organization was migrated to the new Azure DevOps platform, use the dev.azure.com domain format instead of the old visualstudio.com path—this avoids potential endpoint mismatches:

https://dev.azure.com/mine/_apis/git/repositories/ad3d7615-6e4a-4988-bd3f-ca80d7ec9791/pushes?api-version=7.1

4. Confirm oldObjectId is Valid

Ensure branchObjectId is the latest commit ID of the master branch you’re targeting. If this ID points to an outdated commit (not the current head of the branch), it can cause unexpected errors—while this usually returns a 409 Conflict, it’s worth verifying via the Branches API to rule out edge cases.

5. Validate File Path and Change Details

  • Double-check the file path: Repository paths are case-sensitive, and leading slashes might not be required. For example, if the file lives at files/filename.js (not /files/filename.js), adjust the path accordingly.
  • Confirm the file actually exists at that path in the master branch—trying to edit a non-existent file can also trigger 404-like behavior.

6. Check PAT Permissions

While permission issues typically return 403 Forbidden, it’s worth confirming your Personal Access Token (PAT) has the right permissions:

  • Go to your Azure DevOps organization settings > Personal Access Tokens
  • Ensure the token has Code (Contribute) or Code (Full access) permissions
  • Verify the token hasn’t expired

Bonus: Avoid Deadlocks with await

Using .Result on PostAsync can cause deadlocks in some contexts. Switch to await for cleaner, safer code:

using (HttpResponseMessage response = await client.PostAsync(url, content))
{
    response.EnsureSuccessStatusCode();
    string responseBody = await response.Content.ReadAsStringAsync();
    responseBodyAction.Invoke(responseBody);
}

内容的提问来源于stack exchange,提问作者René

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:54:44