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

VSTS REST API不支持POST请求:创建仓库文件失败

Troubleshooting "The requested resource does not support HTTP method 'POST'" when creating files via VSTS REST API

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-Type header to application/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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:04:41