Alex Pilon
Alex Pilon

Reputation: 103

VSTS REST API not accepting POST request

I'm trying to use VSTS's REST API to create a new file in a repository. I've used Microsoft's documentation, https://www.visualstudio.com/en-us/docs/integrate/api/git/pushes#add-a-text-file, but I keep getting an error saying the following:

The requested resource does not support http method 'POST'.

Here's the request I'm sending to the API:

POST https://company.visualstudio.com/DefaultCollection/project_ID/_apis/git/repositories/repository_ID/pushes?api-version=1.0

My credentials are properly configured, as it's getting to the resource (using basic authentication with a PAT). The project and repo ID were gathered using a similar GET request, which worked fine. I've tested this request using POSTMAN, and it blurts out the error mentioned above. I've made sure to configure the header as application/json, but it does nothing.

Also tried different versions of the API, always the same error.

Is there something I'm missing ? The documentation clearly states that this should work using POST, so I'm really confused.

Thanks in advance.

Upvotes: 0

Views: 1129

Answers (1)

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30442

Please note that the project_ID should not be included in the URL.

Just try below REST API:

POST https://company.visualstudio.com/_apis/git/repositories/{repository ID}/pushes?api-version=4.1-preview.2

Content-Type: application/json

{"refUpdates":[{"name":"refs/heads/Dev","oldObjectId":"53e349753ff5a081346babe32255eb6c5da63a5a"}],"commits":[{"changes":[{"changeType":1,"item":{"path":"/test.txt"},"newContent":{"content":"123test","contentType":0}}],"comment":"Added file test.txt"}]}

Note that you need to use the latest commit SHA as the oldObjectId every time to run it, otherwise it will response 409 Conflict error.

Follow below steps to get the latest commit SHA:

  1. Go to the Code page > Files
  2. Choose a repository and branch
  3. Select the root level (repository name) > History
  4. Click of the first commit > Copy full SHA

enter image description here

Upvotes: 1

Related Questions