-1

Azure DevOps has a REST API: https://learn.microsoft.com/en-us/rest/api/azure/devops/?view=azure-devops-rest-7.1

How do I access it using curl.exe or PowerShell without additional support libraries?

zumalifeguard
  • 8,648
  • 5
  • 43
  • 56
  • Did you look at the documentation? – Daniel Mann Nov 23 '22 at 19:01
  • Yes. The documentation for the REST API doesn't show you how to access the API using curl.exe and PowerShell. If you want to point me to the Microsoft documentation that shows that, that'd be great. – zumalifeguard Nov 25 '22 at 22:59
  • Did you do any additional research? A quick search of Stack Overflow showed me this: https://stackoverflow.com/questions/67472498/need-to-encode-pat-to-use-it-in-rest-api-for-azure-devops-in-a-bash-script-stru, which includes instructions for using `curl`. – Daniel Mann Nov 26 '22 at 00:03
  • @DanielMann those wouldn't make sense to Windows programmers. This is specifically for curl.exe in Windows. Plus, some people prefer to use PowerShell on Windows. – zumalifeguard Nov 27 '22 at 16:21

1 Answers1

-1

This instructions are for Windows, although they can be used in other platforms with slight modification.

Below, replace with your own values for:

  • {organization}
  • {project}
  • {team}
  • {pat}

You can create a PAT in your organization's portal. Using your browser, go to: (replace {organization}):

https://dev.azure.com/{organization}/_usersSettings/tokens
Using Command Prompt

If using Command Prompt (cmd.exe), curl.exe can be used. It is part of Windows (10+). Also works in PowerShell, but you have to spell out curl.exe, not just curl. The commands below output the results to the console. You can pipe it to a file, or use the -o option in curl to specify a file to output to.

Using PoowerShell

If using PowerShell, you use the Invoke-RestMethod command. In the below commands, the result of the command is placed in a variable, $result, which you can use after the invocation. The commands below also assume variables $pat and $header are initialized as follows:

$pat = "{pat}"
$header = @{authorization = "Basic $([System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)")))"}

Example getting a work item by ID (Work Items - Get Work Item):

Using curl:
curl.exe -u :{pat} -H "Content-Type: application/json" https://dev.azure.com/{organization}/_apis/wit/workItems/{id}
Using PowerShell:
$url = "https://dev.azure.com/{organization}/_apis/wit/workItems/{id}"
$entries = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header

Quick display of the above output:

$entries.Fields | Format-Table system.workitemtype, system.title
zumalifeguard
  • 8,648
  • 5
  • 43
  • 56