通过REST API更新VSTS Git文件失败求助(404错误)
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
masterbranch—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é

