VSTS REST API不支持POST请求:创建仓库文件失败
Let's break down why you're hitting this error and how to fix it—this is a common gotcha with the Azure DevOps (formerly VSTS) Git APIs, even when you've got auth and IDs right.
1. Double-check your request endpoint
The most likely culprit is an incorrect URL. The correct endpoint for creating a file via a push is:
https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pushes?api-version=7.1-preview.1
Make sure you're not accidentally targeting a different endpoint (like /files instead of /pushes)—the "POST not supported" error often pops up when you're hitting a resource that only accepts GET/PUT.
2. Verify your request body structure
Even if the endpoint is right, a malformed request body can cause unexpected method errors. Your POST body must include both refUpdates and commits sections. Here's a minimal working example:
{ "refUpdates": [ { "name": "refs/heads/main", // Target branch "oldObjectId": "0000000000000000000000000000000000000000" // Use all zeros for new/empty branches; use latest commit ID for existing branches } ], "commits": [ { "comment": "Add new text file", "changes": [ { "changeType": "add", "item": { "path": "/my-new-file.txt" // Full path in repo }, "newContent": { "content": "Your file content here", "contentType": "rawtext" } } ] } ] }
If you're missing either refUpdates or commits, the API might reject the request with a misleading method error instead of a body validation error.
3. Confirm HTTP headers and method
- Ensure you're definitely using POST (not PUT or GET) for this endpoint.
- Set the
Content-Typeheader toapplication/json—this is easy to forget, and missing it can cause the API to misinterpret your request. - Double-check your PAT auth: The header should be
Authorization: Basic {base64-encoded string}where the string is:{your-pat}(note the leading colon before your PAT).
4. Check your API version
Older API versions might not support the POST operation on the /pushes endpoint. Stick to a recent preview or GA version, like 7.1-preview.1 or higher. Outdated versions can throw method-not-supported errors even if everything else is correct.
Quick debugging tip
Use a tool like Postman or curl to test the request directly—this helps isolate whether the issue is in your code implementation or the request itself. Look for additional error details in the response body (sometimes the API includes more context than just the method error message).
内容的提问来源于stack exchange,提问作者Alex Pilon

