Skip to content
Advertisement

Azure DevOps: How to download a file from git using REST API?

Following the example here: https://learn.microsoft.com/en-us/rest/api/azure/devops/git/items/get?view=azure-devops-rest-6.1

I can query a dev ops organization and get a response like so:

{
  "count": 1,
  "value": [
    {
      "objectId": "61a86fdaa79e5c6f5fb6e4026508489feb6ed92c",
      "gitObjectType": "blob",
      "commitId": "23d0bc5b128a10056dc68afece360d8a0fabb014",
      "path": "/MyWebSite/MyWebSite/Views/Home/_Home.cshtml",
      "url": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/items/MyWebSite/MyWebSite/Views/Home/_Home.cshtml?versionType=Branch&versionOptions=None"
    }
  ]
}

How can I use Python to, I guess, download that url? The file should be an XML file. I want to read (download) it directly from Python.

If I take the url returned above and insert it into yet another GET request, I get sent in loops basically.

Edit:

I figured out that if I paste the URL that it gives me, I can download the file with my web browser. However, when I insert that same URL into a new request, I get the same meta-data over and over, trying:

response = requests.get(url=(url), headers=headers, stream=True)
response.text
response.content

response = requests.get(url=(url), headers=headers, stream=False)
response.text
response.content

Advertisement

Answer

I have solved the problem, as both comments and previous answer are incorrect and will not lead you to the right answer for future visitors.

Following the example here as originally posted, it presumes that download=True: https://learn.microsoft.com/en-us/rest/api/azure/devops/git/items/get?view=azure-devops-rest-6.1

However, you then need to query the file of interest and set this flag: &includeContent=true to actually get the file content from the devops GIT.

Like so:

https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/items?scopePath=/MyWebSite/MyWebSite/Views/Home/_Home.cshtml&includeContent=true&api-version=6.1-preview.1

or you will send as many GET requests as you like and get nothing of value in return.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement