Original source - Azure DevOps Tip - Find private APIs
Often working with Azure DevOps, I hit a wall trying to automate some tasks but there are no REST API's made public yet.
It was one of those task of automating creation of Environments in multi-stage YAML based pipelines in AzDO.
Quick research reveals that this has been requested in uservoice (please upvote).
Let's see one of the very simple ways to discover some of these APIs.
That's mostly it, fire up postman/PowerShell to make the API call to test this.
It was one of those task of automating creation of Environments in multi-stage YAML based pipelines in AzDO.
Quick research reveals that this has been requested in uservoice (please upvote).
Let's see one of the very simple ways to discover some of these APIs.
F12 Developer tools to rescue
Using your browser's developers tools you can actually inspect the HTTP requests being made while performing an action in the web portal.
Let's do this.
Let's click on the "Create Environment" button, fill out some dummy values, hit create and keep an eye on the network tab.
We see some activity, it might take you some time to walk through what happened but in this case the top activity named "environments" has the required details.
See below and note the URL & method used:
With a HTTP POST method we often specify a request JSON body. You can see those details in the same activity under "request payload". The request payload matches the input in the web form.
That's mostly it, fire up postman/PowerShell to make the API call to test this.
Invoke-RestMethod with Pwsh
Generate a Personal Access Token in AzDO, typically start with a short lived PAT token with full access and then nail down on the specific permissions you need.
Below is the code snippet, I used with AzDO to hit the REST API endpoint:
But when I execute the above code, it gives me an error
The error thrown is below:
Read the error message, it explains that the api-version is missing. Also, looking back at the capture and see where the api-version was specified.
It is clear that the api-version in specified in the headers, this means we can do the same in our code snippet from before and modify it to get working.
Here I go, this finally works and using the similar API endpoint I can fetch the environments as well (GET request).
The error thrown is below:
Read the error message, it explains that the api-version is missing. Also, looking back at the capture and see where the api-version was specified.
It is clear that the api-version in specified in the headers, this means we can do the same in our code snippet from before and modify it to get working.
Here I go, this finally works and using the similar API endpoint I can fetch the environments as well (GET request).