It's possible to generate a link to raw content of the file in GitHub, is it possible to do with VSTS/DevOps?
Sure, here's the rests call needed:
GET https://feeds.dev.azure.com/{organization}/_apis/packaging/Feeds/{feedId}/packages/{packageId}?includeAllVersions={includeAllVersions}&includeUrls={includeUrls}&isListed={isListed}&isRelease={isRelease}&includeDeleted={includeDeleted}&includeDescription={includeDescription}&api-version=5.0-preview.1
Even after reading the existing answers, I still struggled with this a bit, so I wanted to leave a bit more of a thorough response.
As others have said, the pattern is (query split onto separate lines for ease of reading):
https://dev.azure.com/{{organization}}/{{project}}/_apis/sourceProviders/{{providerName}}/filecontents
?repository={{repository}}
&path={{path}}
&commitOrBranch={{commitOrBranch}}
&api-version=7.0
But how do you find the values for these variables? If you go into your Azure DevOps, choose Repos > Files from the left navigation, and select a particular file, your current url should look something like this:
https://dev.azure.com/{{organization}}/{{project}}/_git/{{repository}}?path=%2Fpackage.json
You should use those values for organization, project, and repository. For path, you'll see an HTTP encoded version of the unix file path. %2F
is the HTTP encoding for /
, so that path is actually just /package.json
(a tool like Postman will do that encoding for you).
Commit or branch is pretty self explanatory; you either know what you want for this value or you should use master
. I have "hard-coded" the api version in the above url because that's what the documentation currently points to.
For the last variable, you need providerName
. In short, you should probably use TfsGit
. I got this value from looking through the list of source providers and looking for one with a value of true
for supportedCapabilities.queryFileContents
.
However, if you just request this URL you'll get a "203 Non-Authoritative Information" response back because you still need to authenticate yourself. Referring again to the same documentation, it says to use Basic auth with any value for the username and a personal access token for the password. You can create a personal access token at https://dev.azure.com/{{organization}}/_usersSettings/tokens
; ensure that it has the Build: Read permission.
If you're unfamiliar with this sort of thing, again Postman is super helpful with getting these requests working before you get into the code.
So if you have a repository with a src
directory at the root, and you're trying to get the file contents of src/package.json
, your URL should look something like:
https://dev.azure.com/{{organization}}/{{project}}/_apis/sourceProviders/TfsGit/filecontents?repository={{repository}}&commitOrBranch=master&api-version={{api-version}}&path=src%2Fpackage.json
And don't forget the basic auth!
api-version=7.0
if you like. The rest of the URL stays the same. –
Phillisphilly Sure, here's the rests call needed:
GET https://feeds.dev.azure.com/{organization}/_apis/packaging/Feeds/{feedId}/packages/{packageId}?includeAllVersions={includeAllVersions}&includeUrls={includeUrls}&isListed={isListed}&isRelease={isRelease}&includeDeleted={includeDeleted}&includeDescription={includeDescription}&api-version=5.0-preview.1
You can obtain the raw URL using chrome.
Turn on Developer tools and view the Network tab.
Navigate to view the required file in the DevOps portal (Content panel). Once the content view is visible check the network tab again and find the URL which starts with "Items?Path", this is json response which contains the required "url:" element.
I was able to get the raw contents of a file using this URL.
GET https://dev.azure.com/{organization}/{project}/_apis/sourceProviders/{providerName}/filecontents?serviceEndpointId={serviceEndpointId}&repository={repository}&commitOrBranch={commitOrBranch}&path={path}&api-version=5.0-preview.1
I got this from here.
Most answers address this well, but in context of a public repo with anonymous access the api is different. Here is the one that works in such a scenario:
https://dev.azure.com/{{organization_name}}/{{project_name}}/_apis/git/repositories/{{repo_name_encoded}}/items?scopePath={{path_to_your_file}}&api-version=6.0
This is the exact equivalent of the "raw" url provided by Github.
So the idea is not to clone the repo but to pick one particular file from the repo, so here is an example of how to do so.
Let us assume this is your Azure DevOps Repo, accessible from the browser.
https://dev.azure.com/<Org>/<Project>/_git/<Repo>?path=<Path>
To access or get the file in your terminal, use this URI.
https://dev.azure.com/<Org>/<Project>/_apis/sourceProviders/TfsGit/filecontents?repository=<Repo>&commitOrBranch=master&api-version=6.0&path=<Path>
Windows
Invoke-WebRequest https://dev.azure.com/<Org>/<Project>/_apis/sourceProviders/TfsGit/filecontents?repository=<Repo>&commitOrBranch=master&api-version=6.0&path=<Path> -Credential <PSCredential>
or
$pair = "<username>:<password>"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
Authorization = $basicAuthValue
}
$repo = https://dev.azure.com/<Org>/<Project>/_apis/sourceProviders/TfsGit/filecontents?repository=<Repo>&commitOrBranch=master&api-version=6.0&path=<Path>
$downloadPath = "C:\Temp\$fileName"
Invoke-WebRequest -Uri $uri -Headers $Headers
Linux
curl -u "<username:password>" https://dev.azure.com/<Org>/<Project>/_apis/sourceProviders/TfsGit/filecontents?repository=<Repo>&commitOrBranch=master&api-version=6.0&path=<Path>
Note:
The username will be your Azure DevOps username and the password will be your PAT (personal access token)
Drag the filename from the attachments windows and drop it in to any other MS application to get the raw URL or linked filename.
I am fairly new to this and had an issue accessing a raw file in an Azure DevOps Repo. It's straightforward in Github.
I wanted to download a file in CMD and BASH using Curl. First I browsed to the file contents in the browser make a note of the bold sections: https://dev.azure.com/**myOrg**/_git/**myProjectName**?path=%2F**MyFileName.ps1**
I then constructed the URL similar to what @Zach posted above.
Now when I paste the above URL in the browser it displays the content in RAW form similar to GitHub. The difference was I had to setup a PAT (Personal Access Token) in My Azure DevOps account then authenticate the URL in DOS/BASH example below:
curl -u "<username>:<password>" "https://dev.azure.com/myOrg/myProjectName/_apis/sourceProviders/TfsGit/filecontents?repository=myProjectName&commitOrBranch=master&api-version=5.0-preview.1&path=%2FMyFileName.ps1" -# -L -o MyFileName.ps1
Another way that may be helpful if you want to quickly get the raw URL for a specific file that you are browsing:
- install the browser extension named "Undisposition"
- from the dot menu (top right) choose "Download": the file will open in a new browser tab from which you can copy the URL
(edit: unfortunately this will only work for file types that the browser knows how to open, otherwise it will still offer to download it...)
© 2022 - 2025 — McMap. All rights reserved.